diff --git a/src/article-render.ts b/src/article-render.ts index 8728237..109ac31 100644 --- a/src/article-render.ts +++ b/src/article-render.ts @@ -373,10 +373,26 @@ export class ArticleRender implements MDRendererCallback { if (fmAuthor) res.author = fmAuthor; res.digest = this.getFrontmatterValue(frontmatter, keys.digest); res.content_source_url = this.getFrontmatterValue(frontmatter, keys.content_source_url); - res.cover = this.getFrontmatterValue(frontmatter, keys.cover) || frontmatter['cover'] || frontmatter['image']; - // frontmatter 给出的 cover/image 为空字符串时视为未设置 - if (typeof res.cover === 'string' && res.cover.trim() === '') { - res.cover = undefined; + if (!this.settings.ignoreFrontmatterImage) { + let fmCover = this.getFrontmatterValue(frontmatter, keys.cover) || frontmatter['cover'] || frontmatter['image']; + if (typeof fmCover === 'string') { + fmCover = fmCover.trim(); + if (fmCover === '') { + fmCover = undefined; + } else if (/^https?:\/\//i.test(fmCover)) { + // 远程 URL,保持原样(后续逻辑支持 http 上传) + } else { + // 本地路径:可能是 /img/xxx.png 或 相对路径 foo/bar.png + const clean = fmCover.replace(/^"|"$/g, '').split('#')[0].split('?')[0]; + const base = clean.split('/').pop(); + if (base) { + fmCover = `![[${base}]]`; + } + } + } + res.cover = fmCover; + } else { + res.cover = undefined; // 忽略 frontmatter } res.thumb_media_id = this.getFrontmatterValue(frontmatter, keys.thumb_media_id); res.need_open_comment = frontmatter[keys.need_open_comment] ? 1 : undefined; @@ -407,11 +423,19 @@ export class ArticleRender implements MDRendererCallback { const v = m[2].trim(); if (k === 'title' && !res.title) res.title = v; if (k === 'author' && !res.author) res.author = v; - if ((k === 'image' || k === 'cover') && !res.cover && v) { - // image 可能是路径,取 basename - const clean = v.replace(/^"|"$/g, '').split('#')[0].split('?')[0]; - const base = clean.split('/').pop(); - if (base) res.cover = `![[${base}]]`; + if (!this.settings.ignoreFrontmatterImage) { + if ((k === 'image' || k === 'cover') && !res.cover && v) { + let val = v.replace(/^"|"$/g, '').trim(); + if (val) { + if (/^https?:\/\//i.test(val)) { + res.cover = val; // URL + } else { + const clean = val.split('#')[0].split('?')[0]; + const base = clean.split('/').pop(); + if (base) res.cover = `![[${base}]]`; + } + } + } } } } diff --git a/src/setting-tab.ts b/src/setting-tab.ts index 5c1c4d1..22f7eb9 100644 --- a/src/setting-tab.ts +++ b/src/setting-tab.ts @@ -312,6 +312,17 @@ export class NoteToMpSettingTab extends PluginSettingTab { text.inputEl.setAttr('style', 'width: 360px;'); }); + new Setting(containerEl) + .setName('忽略 frontmatter 封面') + .setDesc('开启后不使用 frontmatter 中 cover/image 字段,封面将按正文首图→gallery→默认封面回退') + .addToggle(toggle => { + toggle.setValue(this.settings.ignoreFrontmatterImage); + toggle.onChange(async (value) => { + this.settings.ignoreFrontmatterImage = value; + await this.plugin.saveSettings(); + }); + }); + new Setting(containerEl) .setName('启用空行渲染') .addToggle(toggle => { diff --git a/src/settings.ts b/src/settings.ts index 09f7b88..e8008dd 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -49,6 +49,8 @@ export class NMPSettings { galleryNumPic: number; // 无图片时的默认封面(wikilink 或 URL 均可) defaultCoverPic: string; + // 是否忽略 frontmatter 中的 cover/image 字段(用户要求:封面不使用 frontmatter image) + ignoreFrontmatterImage: boolean; private static instance: NMPSettings; @@ -84,6 +86,7 @@ export class NMPSettings { this.galleryNumPic = 2; // 默认封面:使用当前笔记同目录下的 cover.png (若存在会被后续流程正常解析;不存在则无效但可被用户覆盖) this.defaultCoverPic = 'cover.png'; + this.ignoreFrontmatterImage = false; } resetStyelAndHighlight() { @@ -117,6 +120,7 @@ export class NMPSettings { galleryPrePath, galleryNumPic, defaultCoverPic, + ignoreFrontmatterImage, } = data; const settings = NMPSettings.getInstance(); @@ -183,6 +187,9 @@ export class NMPSettings { if (defaultCoverPic !== undefined) { settings.defaultCoverPic = String(defaultCoverPic).trim(); } + if (ignoreFrontmatterImage !== undefined) { + settings.ignoreFrontmatterImage = !!ignoreFrontmatterImage; + } settings.getExpiredDate(); settings.isLoaded = true; } @@ -211,6 +218,7 @@ export class NMPSettings { 'galleryPrePath': settings.galleryPrePath, 'galleryNumPic': settings.galleryNumPic, 'defaultCoverPic': settings.defaultCoverPic, + 'ignoreFrontmatterImage': settings.ignoreFrontmatterImage, } }