update at 2025-09-25 22:35:01

This commit is contained in:
douboer
2025-09-25 22:35:01 +08:00
parent a60cdd593c
commit c9ce811ce9
11 changed files with 1192 additions and 21 deletions

View File

@@ -41,9 +41,11 @@ import { stat, readdir } from 'fs/promises';
const FRONT_MATTER_REGEX = /^(---)$.+?^(---)$.+?/ims;
// gallery 配置迁移到 NMPSettingsgalleryPrePath, galleryNumPic
// 匹配示例:{{<gallery dir="/img/guanzhan/1" figcaption="毕业展"/>}}{{<load-photoswipe>}}
// figcaption 可选
const GALLERY_SHORTCODE_REGEX = /{{<gallery\s+dir="([^"]+)"(?:\s+figcaption="([^"]*)")?\s*\/?>}}\s*{{<load-photoswipe>}}/g;
// 匹配示例:{{<gallery dir="/img/guanzhan/1" figcaption="毕业展" mppickall=1/>}}{{<load-photoswipe>}}
// 支持可选 figcaption 以及 mppickall=1/0无引号数字或布尔若 mppickall=1 则选取目录内全部图片
// 说明为保持简单mppickall 只支持 0/1不写或写 0 则按限制数量。
// mppickall 允许mppickall=1 | mppickall='1' | mppickall="1" 0 同理)
const GALLERY_SHORTCODE_REGEX = /{{<gallery\s+dir="([^"]+)"(?:\s+figcaption="([^"]*)")?(?:\s+mppickall=(?:"(1|0)"|'(1|0)'|(1|0)))?\s*\/?>}}\s*{{<load-photoswipe>}}/g;
// 块级 gallery
// {{<gallery>}}\n{{<figure src="/img/a.png" caption=".." >}}\n...\n{{</gallery>}}
// 需要提取所有 figure 的 src basename 生成多行 wikilink
@@ -70,16 +72,17 @@ function pickImages(all: string[], limit: number): string[] {
async function transformGalleryShortcodes(md: string, prePath: string, numPic: number): Promise<string> {
// 逐个替换(异步)—— 使用 replace + 手动遍历实现
const matches: { full: string; dir: string; caption?: string }[] = [];
md.replace(GALLERY_SHORTCODE_REGEX, (full, dir, caption) => {
matches.push({ full, dir, caption });
const matches: { full: string; dir: string; caption?: string; pickAll?: boolean }[] = [];
md.replace(GALLERY_SHORTCODE_REGEX, (full, dir, caption, q1, q2, plain) => {
const flag = q1 || q2 || plain; // 三个捕获组任选其一
matches.push({ full, dir, caption, pickAll: flag === '1' });
return full;
});
let result = md;
for (const m of matches) {
const absDir = path.join(prePath, m.dir.replace(/^\//, '')); // 拼接绝对路径
const imgs = await listLocalImages(absDir);
const picked = pickImages(imgs, numPic);
const picked = m.pickAll ? imgs : pickImages(imgs, numPic);
if (picked.length === 0) {
// 无图则清空短代码(或保留原样,这里按需求替换为空)
result = result.replace(m.full, '');