From a5fceda7d04091aaaea2f661a33e377a1c744a9b Mon Sep 17 00:00:00 2001 From: nan Date: Tue, 21 Apr 2026 18:22:45 +0800 Subject: [PATCH] feat: Restrict GitHub Actions to main branch and enhance settings for folder structure and asset handling --- .github/workflows/lint.yml | 4 +-- src/main.ts | 59 ++++++++++++++++++++++++++------------ src/settings.ts | 28 ++++++++++++++++-- tsconfig.json | 1 - 4 files changed, 69 insertions(+), 23 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 7748ceb..3ad582f 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -2,9 +2,9 @@ name: Node.js build on: push: - branches: ["**"] + branches: ["main"] pull_request: - branches: ["**"] + branches: ["main"] jobs: build: diff --git a/src/main.ts b/src/main.ts index 8f8d3d6..0cee67f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -95,11 +95,30 @@ export default class VitePressExporterPlugin extends Plugin { // 1. 扫描并复制所有资源文件(传入文件对象用于路径解析) const updatedContent = await this.processAssets(content, file); - // 2. 处理 canvas 引用(暂时保留,后续实现) - // const finalContent = await this.processCanvases(updatedContent); + // 2. 计算输出路径 + let docsPath: string; + const fileNameWithoutExt = file.name.replace('.md', ''); + const filePathWithoutExt = file.path.replace('.md', ''); - // 3. 写入到 docs 目录(使用 binary 模式保留原始格式) - const docsPath = `${this.settings.docsPath}/${file.name}`; + if (this.settings.preserveFolderStructure && this.settings.nestNoteFolder) { + // 保留文件夹 + 嵌套: notes/some-note.md > docs/notes/some-note/index.md + docsPath = `${this.settings.docsPath}/${filePathWithoutExt}/index.md`; + } else if (this.settings.preserveFolderStructure) { + // 保留文件夹结构: notes/some-note.md > docs/notes/some-note.md + docsPath = `${this.settings.docsPath}/${file.path}`; + } else if (this.settings.nestNoteFolder) { + // 嵌套文件夹: notes/some-note.md > docs/some-note/index.md + docsPath = `${this.settings.docsPath}/${fileNameWithoutExt}/index.md`; + } else { + // 默认: notes/some-note.md > docs/some-note.md + docsPath = `${this.settings.docsPath}/${file.name}`; + } + + // 3. 确保目标目录存在 + const targetDir = docsPath.substring(0, docsPath.lastIndexOf('/')); + await vault.adapter.mkdir(targetDir); + + // 4. 写入到 docs 目录(使用 binary 模式保留原始格式) const updatedBuffer = new TextEncoder().encode(updatedContent); await vault.adapter.writeBinary(docsPath, updatedBuffer.buffer); @@ -244,14 +263,17 @@ export default class VitePressExporterPlugin extends Plugin { console.log('Trying to replace links for paths:', possiblePaths); - // 替换 markdown 格式: ![](path) -> ![](assets/newfile.png) + // 使用根路径: /image.png + const assetsRef = `/${newFileName}`; + + // 替换 markdown 格式: ![](path) -> ![](/filename.png) const markdownRegex = /!\[([^\]]*)\]\(([^)]+)\)/g; content = content.replace(markdownRegex, (match, alt, path) => { // 检查这个路径是否匹配我们的目标文件 for (const testPath of possiblePaths) { if (path === testPath || path === testPath.replace(/^\//, '')) { - console.log(`Found matching link: ${path} -> ${this.settings.assetsPath}/${newFileName}`); - return `![](${this.settings.assetsPath}/${newFileName})`; + console.log(`Found matching link: ${path} -> ${assetsRef}`); + return `![](${assetsRef})`; } } return match; @@ -265,8 +287,8 @@ export default class VitePressExporterPlugin extends Plugin { for (const testPath of possiblePaths) { if (cleanLink === testPath || cleanLink === testPath.replace(/^\//, '')) { - console.log(`Found matching wikilink: ${cleanLink} -> ${this.settings.assetsPath}/${newFileName}`); - return `![[${this.settings.assetsPath}/${newFileName}]]`; + console.log(`Found matching wikilink: ${cleanLink} -> ${assetsRef}`); + return `![[${assetsRef}]]`; } } return match; @@ -519,21 +541,23 @@ export default class VitePressExporterPlugin extends Plugin { console.log('Trying to replace canvas links for paths:', possiblePaths); - // 替换 markdown 格式: !(canvas) -> + // 使用根路径: /image.png + const assetsRef = `/${imageFileName}`; + + // 替换 markdown 格式: !(canvas) -> const markdownRegex = /!\[([^\]]*)\]\(([^)]+)\)/g; - content = content.replace(markdownRegex, (match, alt, path) => { + content = content.replace(markdownRegex, (match, _alt, path) => { // 检查这个路径是否匹配我们的 canvas 文件 for (const testPath of possiblePaths) { if (path === testPath || path === testPath.replace(/^\//, '')) { - const relativePath = this.settings.assetsPath.replace('docs/', ''); - console.log(`Found matching canvas link: ${path} -> `); - return ``; + console.log(`Found matching canvas link: ${path} -> `); + return ``; } } return match; }); - // 替换 wikilink 格式: ![[canvas]] -> + // 替换 wikilink 格式: ![[canvas]] -> const wikilinkRegex = /!\[\[([^\]]+)\]\]/g; content = content.replace(wikilinkRegex, (match, link) => { // 移除可能的别名字段 @@ -541,9 +565,8 @@ export default class VitePressExporterPlugin extends Plugin { for (const testPath of possiblePaths) { if (cleanLink === testPath || cleanLink === testPath.replace(/^\//, '')) { - const relativePath = this.settings.assetsPath.replace('docs/', ''); - console.log(`Found matching canvas wikilink: ${cleanLink} -> `); - return ``; + console.log(`Found matching canvas wikilink: ${cleanLink} -> `); + return ``; } } return match; diff --git a/src/settings.ts b/src/settings.ts index 6e1cb5f..0320056 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -4,11 +4,15 @@ import VitePressExporterPlugin from "./main"; export interface VitePressExporterSettings { docsPath: string; assetsPath: string; + nestNoteFolder: boolean; + preserveFolderStructure: boolean; } export const DEFAULT_SETTINGS: VitePressExporterSettings = { docsPath: 'docs', - assetsPath: 'docs/public' + assetsPath: 'docs/public', + nestNoteFolder: true, + preserveFolderStructure: true } // 设置面板 @@ -48,5 +52,25 @@ export class VitePressExporterSettingTab extends PluginSettingTab { this.plugin.settings.assetsPath = value; await this.plugin.saveSettings(); })); + + new Setting(containerEl) + .setName('Nest note folder') + .setDesc('Create a folder for each note (e.g., notes/some-note.md > docs/some-note/index.md)') + .addToggle(toggle => toggle + .setValue(this.plugin.settings.nestNoteFolder) + .onChange(async (value) => { + this.plugin.settings.nestNoteFolder = value; + await this.plugin.saveSettings(); + })); + + new Setting(containerEl) + .setName('Preserve folder structure') + .setDesc('Keep original folder structure (e.g., notes/some-note.md > docs/notes/some-note.md)') + .addToggle(toggle => toggle + .setValue(this.plugin.settings.preserveFolderStructure) + .onChange(async (value) => { + this.plugin.settings.preserveFolderStructure = value; + await this.plugin.saveSettings(); + })); } -} \ No newline at end of file +} diff --git a/tsconfig.json b/tsconfig.json index 45b1c4f..222535d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,5 @@ { "compilerOptions": { - "ignoreDeprecations": "6.0", "baseUrl": "src", "inlineSourceMap": true, "inlineSources": true,