大佬们,自定义标题设置怎么实现?

大佬们,篇幅较长的文章中有这样一个需求,想自动化去处理标题,比如可以设置
H1: A、B、C、D、E、……
H2: 1、2、3、4、5、……
H3: 1.1、1.2、1.3、……
H4: 1) 、2)、3)、……
……

设置完成后,希望处理的标题如下:

One

One-One

Two

Two-Two

Two

处理后的标题按照上面设置的标题列表自动生成:

A、One

1、One-One

B、Two

2、Two-Two

尝试了number headings,可以达到效果但无法自定义只能1.1.1.1这样累加,大佬们有无更灵活点的配置方法?求指教!

同问,这个问题我想很多中文用户需要的。而且最好是能自动按照顺序编号,比如:

一 标题1
二 标题2
三 标题3

当删掉“二 标题2“时,“三 标题3”自动修改为“二 标题3”

OneNote就有这个功能,非常方便。这样各级标题的序号上就不用我们去修改了

可以用dataviewjs实现,比如把下面的脚本放到待处理的文档中,在你编辑过程中,它会实时处理,处理结果存放到本文件所在目录下,名为:{当前文件名}-format.md

```dataviewjs
dv.paragraph('已处理完毕,结果存放在以下路径中:')
dv.el('hr', '')
dv.paragraph(dv.current().file.name+'-format.md')
const a = dv.el('a', '立即查看')
a.onclick = () => {
    app.workspace.getLeaf('tab')
    .openFile(app.vault.getAbstractFileByPath(dv.current().file.name+'-format.md'));
}
const content = await dv.io.load(dv.current().file.name+'.md')
const map = {
    '#': ['A', 'B', 'C', 'D', 'E'],
    '##': [1, 2, 3, 4, 5],
    '###': ['1.1', '1.2', '1.3'],
    '####': ['1)', '2)', '3)'],
};

const counterMap = {
    '#': 0,
    '##': 0,
    '###': 0,
    '####': 0,
    '#####': 0,
    '######': 0,
};

// 注意:这个函数由AI生成,请严格测试后使用
function replaceHeadingsWithCount(text, mapping, counter) {
    const headingRegex = /(?<=^|\n)(#{1,6})\s*(.*)/gm;

    let result = text;
    let match;
    while ((match = headingRegex.exec(text)) !== null) {
        const hashes = match[1];
        const content = match[2].trim();

        // 获取当前级别的计数器并递增
        let currentCount = counter[hashes]++;
        
        // 确保计数器不会超出映射表的范围
        if (currentCount >= mapping[hashes].length) {
            // 如果超出,重置计数器并给出警告或处理逻辑
            console.warn(`警告: 映射表中#${hashes.length}的数量不足以覆盖所有标题。`);
            currentCount = currentCount % mapping[hashes].length; // 简单示例:循环使用
        }

        // 构建新标题
        const replacement = mapping[hashes][currentCount];
        const newTitle = `${hashes} ${replacement}${content ? `、${content}` : ''}`;
        result = result.replace(match[0], newTitle);
    }

    return result;
}

const replaceText = content.split('```dataviewjs')[0];

const newContent = replaceHeadingsWithCount(replaceText, map, counterMap);
console.log(replaceText, newContent);
app.vault.adapter.write(dv.current().file.name+'-format.md', newContent);

```

处理结果:

注意:这个replaceHeadingsWithCount函数由AI生成,请严格测试后使用!!!