feat: Restrict GitHub Actions to main branch and enhance settings for folder structure and asset handling
Some checks failed
Node.js build / build (20.x) (push) Failing after 22s
Node.js build / build (22.x) (push) Failing after 14m17s

This commit is contained in:
2026-04-21 18:22:45 +08:00
parent 113c3b668f
commit a5fceda7d0
4 changed files with 69 additions and 23 deletions

View File

@@ -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) -> <Viewer src="assets/image.png" />
// 使用根路径: /image.png
const assetsRef = `/${imageFileName}`;
// 替换 markdown 格式: !(canvas) -> <Viewer src="/image.png" />
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} -> <Viewer src="${relativePath}/${imageFileName}" />`);
return `<Viewer src="${relativePath}/${imageFileName}" />`;
console.log(`Found matching canvas link: ${path} -> <Viewer src="${assetsRef}" />`);
return `<Viewer src="${assetsRef}" />`;
}
}
return match;
});
// 替换 wikilink 格式: ![[canvas]] -> <Viewer src="assets/image.png" />
// 替换 wikilink 格式: ![[canvas]] -> <Viewer src="/image.png" />
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} -> <Viewer src="${relativePath}/${imageFileName}" />`);
return `<Viewer src="${relativePath}/${imageFileName}" />`;
console.log(`Found matching canvas wikilink: ${cleanLink} -> <Viewer src="${assetsRef}" />`);
return `<Viewer src="${assetsRef}" />`;
}
}
return match;