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