update at 2025-09-22 21:02:14

This commit is contained in:
douboer
2025-09-22 21:02:14 +08:00
parent b70d02fb84
commit 855d06e727
3 changed files with 52 additions and 9 deletions

View File

@@ -373,10 +373,26 @@ export class ArticleRender implements MDRendererCallback {
if (fmAuthor) res.author = fmAuthor; if (fmAuthor) res.author = fmAuthor;
res.digest = this.getFrontmatterValue(frontmatter, keys.digest); res.digest = this.getFrontmatterValue(frontmatter, keys.digest);
res.content_source_url = this.getFrontmatterValue(frontmatter, keys.content_source_url); res.content_source_url = this.getFrontmatterValue(frontmatter, keys.content_source_url);
res.cover = this.getFrontmatterValue(frontmatter, keys.cover) || frontmatter['cover'] || frontmatter['image']; if (!this.settings.ignoreFrontmatterImage) {
// frontmatter 给出的 cover/image 为空字符串时视为未设置 let fmCover = this.getFrontmatterValue(frontmatter, keys.cover) || frontmatter['cover'] || frontmatter['image'];
if (typeof res.cover === 'string' && res.cover.trim() === '') { if (typeof fmCover === 'string') {
res.cover = undefined; 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.thumb_media_id = this.getFrontmatterValue(frontmatter, keys.thumb_media_id);
res.need_open_comment = frontmatter[keys.need_open_comment] ? 1 : undefined; res.need_open_comment = frontmatter[keys.need_open_comment] ? 1 : undefined;
@@ -407,14 +423,22 @@ export class ArticleRender implements MDRendererCallback {
const v = m[2].trim(); const v = m[2].trim();
if (k === 'title' && !res.title) res.title = v; if (k === 'title' && !res.title) res.title = v;
if (k === 'author' && !res.author) res.author = v; if (k === 'author' && !res.author) res.author = v;
if (!this.settings.ignoreFrontmatterImage) {
if ((k === 'image' || k === 'cover') && !res.cover && v) { if ((k === 'image' || k === 'cover') && !res.cover && v) {
// image 可能是路径,取 basename let val = v.replace(/^"|"$/g, '').trim();
const clean = v.replace(/^"|"$/g, '').split('#')[0].split('?')[0]; if (val) {
if (/^https?:\/\//i.test(val)) {
res.cover = val; // URL
} else {
const clean = val.split('#')[0].split('?')[0];
const base = clean.split('/').pop(); const base = clean.split('/').pop();
if (base) res.cover = `![[${base}]]`; if (base) res.cover = `![[${base}]]`;
} }
} }
} }
}
}
}
} catch (err) { } catch (err) {
console.warn('fallback frontmatter parse failed', err); console.warn('fallback frontmatter parse failed', err);
} }

View File

@@ -312,6 +312,17 @@ export class NoteToMpSettingTab extends PluginSettingTab {
text.inputEl.setAttr('style', 'width: 360px;'); 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) new Setting(containerEl)
.setName('启用空行渲染') .setName('启用空行渲染')
.addToggle(toggle => { .addToggle(toggle => {

View File

@@ -49,6 +49,8 @@ export class NMPSettings {
galleryNumPic: number; galleryNumPic: number;
// 无图片时的默认封面wikilink 或 URL 均可) // 无图片时的默认封面wikilink 或 URL 均可)
defaultCoverPic: string; defaultCoverPic: string;
// 是否忽略 frontmatter 中的 cover/image 字段(用户要求:封面不使用 frontmatter image
ignoreFrontmatterImage: boolean;
private static instance: NMPSettings; private static instance: NMPSettings;
@@ -84,6 +86,7 @@ export class NMPSettings {
this.galleryNumPic = 2; this.galleryNumPic = 2;
// 默认封面:使用当前笔记同目录下的 cover.png (若存在会被后续流程正常解析;不存在则无效但可被用户覆盖) // 默认封面:使用当前笔记同目录下的 cover.png (若存在会被后续流程正常解析;不存在则无效但可被用户覆盖)
this.defaultCoverPic = 'cover.png'; this.defaultCoverPic = 'cover.png';
this.ignoreFrontmatterImage = false;
} }
resetStyelAndHighlight() { resetStyelAndHighlight() {
@@ -117,6 +120,7 @@ export class NMPSettings {
galleryPrePath, galleryPrePath,
galleryNumPic, galleryNumPic,
defaultCoverPic, defaultCoverPic,
ignoreFrontmatterImage,
} = data; } = data;
const settings = NMPSettings.getInstance(); const settings = NMPSettings.getInstance();
@@ -183,6 +187,9 @@ export class NMPSettings {
if (defaultCoverPic !== undefined) { if (defaultCoverPic !== undefined) {
settings.defaultCoverPic = String(defaultCoverPic).trim(); settings.defaultCoverPic = String(defaultCoverPic).trim();
} }
if (ignoreFrontmatterImage !== undefined) {
settings.ignoreFrontmatterImage = !!ignoreFrontmatterImage;
}
settings.getExpiredDate(); settings.getExpiredDate();
settings.isLoaded = true; settings.isLoaded = true;
} }
@@ -211,6 +218,7 @@ export class NMPSettings {
'galleryPrePath': settings.galleryPrePath, 'galleryPrePath': settings.galleryPrePath,
'galleryNumPic': settings.galleryNumPic, 'galleryNumPic': settings.galleryNumPic,
'defaultCoverPic': settings.defaultCoverPic, 'defaultCoverPic': settings.defaultCoverPic,
'ignoreFrontmatterImage': settings.ignoreFrontmatterImage,
} }
} }