From b2870dc9a2996f850de25a16b788d0d114c0d646 Mon Sep 17 00:00:00 2001 From: RavenHogWarts Date: Sun, 23 Mar 2025 14:25:10 +0800 Subject: [PATCH] Add dotenv support and update VAULT_PATH configuration --- .gitignore | 3 +++ package-lock.json | 14 ++++++++++++++ package.json | 3 +++ scripts/copy-to-vault.mjs | 12 ++++++++++-- src/main.ts | 2 +- 5 files changed, 31 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index d3ac41c..627de30 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,6 @@ Thumbs.db # Exclude zip *.zip + +# env +.env \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 8390a2b..ebba7a1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,9 @@ "name": "obsidian-sample-plugin", "version": "1.0.0", "license": "MIT", + "dependencies": { + "dotenv": "^16.4.7" + }, "devDependencies": { "@types/node": "^16.11.6", "@typescript-eslint/eslint-plugin": "5.29.0", @@ -1005,6 +1008,17 @@ "node": ">=6.0.0" } }, + "node_modules/dotenv": { + "version": "16.4.7", + "resolved": "https://registry.npmmirror.com/dotenv/-/dotenv-16.4.7.tgz", + "integrity": "sha512-47qPchRCykZC03FhkYAhrvwU4xDBFIj1QPqaarj6mdM/hgUzfPHcpkHJOn3mJAufFeeAxAzeGsr5X0M4k6fLZQ==", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://dotenvx.com" + } + }, "node_modules/esbuild": { "version": "0.17.3", "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.17.3.tgz", diff --git a/package.json b/package.json index db4b3b4..7503f42 100644 --- a/package.json +++ b/package.json @@ -22,5 +22,8 @@ "obsidian": "latest", "tslib": "2.4.0", "typescript": "4.7.4" + }, + "dependencies": { + "dotenv": "^16.4.7" } } diff --git a/scripts/copy-to-vault.mjs b/scripts/copy-to-vault.mjs index 19e6f9b..fb407be 100644 --- a/scripts/copy-to-vault.mjs +++ b/scripts/copy-to-vault.mjs @@ -3,13 +3,21 @@ import { copyFile, mkdir } from 'fs/promises'; import { existsSync } from 'fs'; import { join, dirname } from 'path'; import { fileURLToPath } from 'url'; +import dotenv from 'dotenv'; // 获取当前文件的目录 const __dirname = dirname(fileURLToPath(import.meta.url)); const rootDir = join(__dirname, '..'); -// 这里定义你的 Obsidian 库路径 -const VAULT_PATH = 'V:\\Code\\CodeRepos\\plugin-dev-vault'; +// 加载 .env 文件中的环境变量 +dotenv.config(); +// 获取 VAULT_PATH 环境变量 +const VAULT_PATH = process.env.VAULT_PATH; +if (!VAULT_PATH) { + throw new Error( + 'VAULT_PATH is not defined. Please create a .env file in the project root and add the line: VAULT_PATH=/path/to/your/vault' + ); +} const pluginDir = join(VAULT_PATH, '.obsidian', 'plugins', 'easy-copy'); async function copyToVault() { diff --git a/src/main.ts b/src/main.ts index c5325a6..6e88bc7 100644 --- a/src/main.ts +++ b/src/main.ts @@ -135,7 +135,7 @@ export default class EasyCopy extends Plugin { const filename = file.basename; // 1. 检查是否是标题行 - if (curLine.startsWith('#')) { + if (/^#+\s/.test(curLine)) { this.copyHeadingLink(curLine, filename); return; }