refactor: Update setting name from 'Nest note folder' to 'Nest in folder' for clarity
This commit is contained in:
89
src/main.ts
89
src/main.ts
@@ -138,8 +138,8 @@ export default class odoxPlugin extends Plugin {
|
|||||||
async exportCanvasFile(file: TFile) {
|
async exportCanvasFile(file: TFile) {
|
||||||
const { vault } = this.app;
|
const { vault } = this.app;
|
||||||
|
|
||||||
// 1. 渲染 canvas 为图片
|
// 1. 渲染 canvas 为图片(传入当前文件以便渲染后返回)
|
||||||
const renderedImage = await this.renderCanvas(file);
|
const renderedImage = await this.renderCanvas(file, file);
|
||||||
if (!renderedImage) {
|
if (!renderedImage) {
|
||||||
new Notice(`Failed to render canvas: ${file.name}`);
|
new Notice(`Failed to render canvas: ${file.name}`);
|
||||||
return;
|
return;
|
||||||
@@ -235,7 +235,8 @@ export default class odoxPlugin extends Plugin {
|
|||||||
try {
|
try {
|
||||||
const canvasFile = vault.getAbstractFileByPath(canvasPath);
|
const canvasFile = vault.getAbstractFileByPath(canvasPath);
|
||||||
if (canvasFile instanceof TFile) {
|
if (canvasFile instanceof TFile) {
|
||||||
const renderedImage = await this.renderCanvas(canvasFile);
|
// 渲染 canvas(传入 sourceFile 以便渲染后返回)
|
||||||
|
const renderedImage = await this.renderCanvas(canvasFile, sourceFile);
|
||||||
if (renderedImage) {
|
if (renderedImage) {
|
||||||
const imageFileName = canvasFile.name + '.png';
|
const imageFileName = canvasFile.name + '.png';
|
||||||
// 图片保存到 docs/assets/
|
// 图片保存到 docs/assets/
|
||||||
@@ -256,7 +257,7 @@ export default class odoxPlugin extends Plugin {
|
|||||||
console.error(`Failed to render canvas ${canvasPath}:`, error);
|
console.error(`Failed to render canvas ${canvasPath}:`, error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 统一更新所有 canvas 引用
|
// 统一更新所有 canvas 引用
|
||||||
content = this.updateCanvasInContent(content, canvasImageMap, sourceFile);
|
content = this.updateCanvasInContent(content, canvasImageMap, sourceFile);
|
||||||
@@ -347,7 +348,8 @@ export default class odoxPlugin extends Plugin {
|
|||||||
// Use Canvas internal API to export (基于 Obsidian export as image 实现)
|
// Use Canvas internal API to export (基于 Obsidian export as image 实现)
|
||||||
/* eslint-disable */
|
/* eslint-disable */
|
||||||
async renderCanvas(
|
async renderCanvas(
|
||||||
canvasFile: TFile
|
canvasFile: TFile,
|
||||||
|
returnToFile: TFile | null = null
|
||||||
): Promise<Uint8Array | null> {
|
): Promise<Uint8Array | null> {
|
||||||
|
|
||||||
const activeLeaf = this.app.workspace.getLeaf();
|
const activeLeaf = this.app.workspace.getLeaf();
|
||||||
@@ -548,6 +550,11 @@ export default class odoxPlugin extends Plugin {
|
|||||||
canvas.zoomToFitQueued = true;
|
canvas.zoomToFitQueued = true;
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
canvas.onResize();
|
canvas.onResize();
|
||||||
|
|
||||||
|
// 返回到原始文件(如果提供了)
|
||||||
|
if (returnToFile) {
|
||||||
|
await activeLeaf.openFile(returnToFile);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -592,9 +599,7 @@ export default class odoxPlugin extends Plugin {
|
|||||||
|
|
||||||
// 统一更新所有 Canvas 链接
|
// 统一更新所有 Canvas 链接
|
||||||
updateCanvasInContent(content: string, canvasImageMap: Map<string, string>, sourceFile: TFile): string {
|
updateCanvasInContent(content: string, canvasImageMap: Map<string, string>, sourceFile: TFile): string {
|
||||||
// 获取源文件的目录路径
|
const { metadataCache } = this.app;
|
||||||
const sourceDir = sourceFile.path.substring(0, sourceFile.path.lastIndexOf('/'));
|
|
||||||
if (!sourceDir) return content;
|
|
||||||
|
|
||||||
// 使用根路径: /filename.png
|
// 使用根路径: /filename.png
|
||||||
const assetsRef = (imageFileName: string) => `/${imageFileName}`;
|
const assetsRef = (imageFileName: string) => `/${imageFileName}`;
|
||||||
@@ -602,34 +607,12 @@ export default class odoxPlugin extends Plugin {
|
|||||||
// 替换 markdown 格式: !(canvas) -> 模板
|
// 替换 markdown 格式: !(canvas) -> 模板
|
||||||
const markdownRegex = /!\[([^\]]*)\]\(([^)]+)\)/g;
|
const markdownRegex = /!\[([^\]]*)\]\(([^)]+)\)/g;
|
||||||
content = content.replace(markdownRegex, (match, _alt, path) => {
|
content = content.replace(markdownRegex, (match, _alt, path) => {
|
||||||
// 尝试为这个路径找到对应的 canvas 文件
|
// 使用 Obsidian API 解析链接(正确处理相对路径)
|
||||||
for (const [canvasPath, imageFileName] of canvasImageMap) {
|
const resolvedLink = metadataCache.getFirstLinkpathDest(path, sourceFile.path);
|
||||||
const canvasFile = this.app.vault.getAbstractFileByPath(canvasPath);
|
if (resolvedLink && canvasImageMap.has(resolvedLink.path)) {
|
||||||
if (canvasFile instanceof TFile) {
|
const imageFileName = canvasImageMap.get(resolvedLink.path)!;
|
||||||
// 构建可能的路径格式
|
const replacement = this.settings.canvasEmbedTemplate.replace("{url}", assetsRef(imageFileName));
|
||||||
const possiblePaths = [
|
return replacement;
|
||||||
// 完整路径
|
|
||||||
canvasPath,
|
|
||||||
// 相对于源文件的路径
|
|
||||||
canvasPath.substring(sourceDir.length + 1),
|
|
||||||
// URL 编码版本
|
|
||||||
encodeURI(canvasPath),
|
|
||||||
encodeURI(canvasPath.substring(sourceDir.length + 1)),
|
|
||||||
// 文件名(包括和不包括 .canvas 扩展名)
|
|
||||||
canvasFile.name,
|
|
||||||
canvasFile.name.replace('.canvas', ''),
|
|
||||||
encodeURI(canvasFile.name),
|
|
||||||
encodeURI(canvasFile.name.replace('.canvas', ''))
|
|
||||||
];
|
|
||||||
|
|
||||||
// 检查是否匹配
|
|
||||||
for (const testPath of possiblePaths) {
|
|
||||||
if (path === testPath || path === testPath.replace(/^\//, '')) {
|
|
||||||
const replacement = this.settings.canvasEmbedTemplate.replace("{url}", assetsRef(imageFileName));
|
|
||||||
return replacement;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return match;
|
return match;
|
||||||
});
|
});
|
||||||
@@ -640,34 +623,12 @@ export default class odoxPlugin extends Plugin {
|
|||||||
// 移除可能的别名字段
|
// 移除可能的别名字段
|
||||||
const cleanLink = link.split('|')[0]!.trim();
|
const cleanLink = link.split('|')[0]!.trim();
|
||||||
|
|
||||||
// 尝试为这个链接找到对应的 canvas 文件
|
// 使用 Obsidian API 解析链接(正确处理相对路径)
|
||||||
for (const [canvasPath, imageFileName] of canvasImageMap) {
|
const resolvedLink = metadataCache.getFirstLinkpathDest(cleanLink, sourceFile.path);
|
||||||
const canvasFile = this.app.vault.getAbstractFileByPath(canvasPath);
|
if (resolvedLink && canvasImageMap.has(resolvedLink.path)) {
|
||||||
if (canvasFile instanceof TFile) {
|
const imageFileName = canvasImageMap.get(resolvedLink.path)!;
|
||||||
// 构建可能的路径格式
|
const replacement = this.settings.canvasEmbedTemplate.replace("{url}", assetsRef(imageFileName));
|
||||||
const possiblePaths = [
|
return replacement;
|
||||||
// 完整路径
|
|
||||||
canvasPath,
|
|
||||||
// 相对于源文件的路径
|
|
||||||
canvasPath.substring(sourceDir.length + 1),
|
|
||||||
// URL 编码版本
|
|
||||||
encodeURI(canvasPath),
|
|
||||||
encodeURI(canvasPath.substring(sourceDir.length + 1)),
|
|
||||||
// 文件名(包括和不包括 .canvas 扩展名)
|
|
||||||
canvasFile.name,
|
|
||||||
canvasFile.name.replace('.canvas', ''),
|
|
||||||
encodeURI(canvasFile.name),
|
|
||||||
encodeURI(canvasFile.name.replace('.canvas', ''))
|
|
||||||
];
|
|
||||||
|
|
||||||
// 检查是否匹配
|
|
||||||
for (const testPath of possiblePaths) {
|
|
||||||
if (cleanLink === testPath || cleanLink === testPath.replace(/^\//, '')) {
|
|
||||||
const replacement = this.settings.canvasEmbedTemplate.replace("{url}", assetsRef(imageFileName));
|
|
||||||
return replacement;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return match;
|
return match;
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ export class odoxSettingTab extends PluginSettingTab {
|
|||||||
}));
|
}));
|
||||||
|
|
||||||
new Setting(containerEl)
|
new Setting(containerEl)
|
||||||
.setName('Nest note folder')
|
.setName('Nest in folder')
|
||||||
.setDesc('Create a folder for each note (e.g., notes/some-note.md > docs/some-note/index.md)')
|
.setDesc('Create a folder for each note (e.g., notes/some-note.md > docs/some-note/index.md)')
|
||||||
.addToggle(toggle => toggle
|
.addToggle(toggle => toggle
|
||||||
.setValue(this.plugin.settings.nestInFolder)
|
.setValue(this.plugin.settings.nestInFolder)
|
||||||
|
|||||||
Reference in New Issue
Block a user