利用 quickadd 插件实现一键 格式化&去除 标题序号。
序号类型:
- 一级标题国字 (一. 二. ···)
- 其余标题数字 (1.1 1.1.1 ···)
格式化标题序号:
QuickAdd 脚本:
var l = class {
constructor() {
this.chnNumChar = [
"\u96F6",
"\u4E00",
"\u4E8C",
"\u4E09",
"\u56DB",
"\u4E94",
"\u516D",
"\u4E03",
"\u516B",
"\u4E5D",
];
this.chnSmlUnit = ["", "\u5341", "\u767E", "\u5343"];
this.chnBigUnit = ["", "\u4E07", "\u4EBF"];
}
sectionToChinese(t) {
let i = !0,
s = "",
e = "",
r = 0;
for (; t > 0; ) {
let n = t % 10;
n == 0
? i || ((i = !0), (e = this.chnNumChar[n] + e))
: ((i = !1),
(s = this.chnNumChar[n]),
(s += this.chnSmlUnit[r]),
(e = s + e)),
(t = Math.floor(t / 10)),
r++;
}
return e;
}
numberToChinese(t) {
let i = !1,
s = "",
e = "",
r = 0;
if (t == 0) return this.chnNumChar[0];
for (; t > 0; ) {
let n = t % 1e4;
i && (e = this.chnNumChar[0] + e),
(s = this.sectionToChinese(n)),
(s += n != 0 ? this.chnBigUnit[r] : this.chnBigUnit[0]),
(e = s + e),
(i = n < 1e3 && n > 0),
(t = Math.floor(t / 1e4)),
r++;
}
return e;
}
},
u = class {};
function v(a, t) {
return Array.prototype.join.call({ length: t + 1 }, a);
}
var f = class extends u {
constructor(t) {
var i;
if (
(super(),
(this.type = t),
(this.activeLeaf = app.workspace.getActiveFile()),
(this.activeEditor =
(i = app.workspace.activeEditor) == null ? void 0 : i.editor),
!(!this.activeLeaf || !this.activeEditor))
)
switch (((this.count = [0, 0, 0, 0, 0, 0]), t)) {
case "\u8BBE\u7F6E\u6807\u9898\u7F16\u53F7":
(this.select = !1),
(this.activeContent = this.activeEditor.getValue());
break;
case "\u53D6\u6D88\u6807\u9898\u7F16\u53F7":
(this.select = !0),
(this.activeContent = this.activeEditor.getSelection()),
this.activeContent == "" &&
((this.select = !1),
(this.activeContent = this.activeEditor.getValue()));
break;
}
}
clearNumber() {
this.activeContent = this.activeContent.replace(
new RegExp(
"^# [\u4E00|\u4E8C|\u4E09|\u56DB|\u4E94|\u516D|\u4E03|\u516B|\u4E5D|\u5341| ]*\\.? *",
"gm"
),
"# "
);
for (let t = 2; t <= 6; t++)
this.activeContent = this.activeContent.replace(
new RegExp(`^${v("#", t)} [\\d|\\.]* `, "gm"),
v("#", t) + " "
);
}
setNumber() {
var r;
let t = 0,
i = "",
s = new l();
this.clearNumber();
let e = this.activeContent.split(`
`);
for (let n = 0; n < e.length; n++) {
let d = e[n],
p = d.split(" ")[0] + " ",
c = (r = p.match(new RegExp("#", "g"))) == null ? void 0 : r.length;
if (c == null) {
i += `${d}
`;
continue;
}
if (c < t) for (let o = t; o > c; o--) this.count[o - 1] = 0;
this.count[c - 1]++;
let h = "";
if (c == 1) h += `${s.numberToChinese(this.count[c - 1])}.`;
else {
h += `${this.count[0]}`;
for (let o = 2; o <= c; o++) h += `.${this.count[o - 1]}`;
}
(h += " "),
(e[n] = e[n].replace(p, p + h)),
(i += `${e[n]}
`),
(t = c);
}
this.activeContent = i;
}
run() {
if (!this.activeLeaf || !this.activeEditor) return;
let t = this.activeEditor.getCursor();
switch (
((this.activeContent = this.activeContent.replace(
new RegExp("\\\\n", "g"),
"\u25A2"
)),
this.type)
) {
case "\u8BBE\u7F6E\u6807\u9898\u7F16\u53F7":
this.setNumber();
break;
case "\u53D6\u6D88\u6807\u9898\u7F16\u53F7":
this.clearNumber();
break;
}
(this.activeContent = this.activeContent.replace(
new RegExp("\u25A2", "g"),
"\\n"
)),
(this.activeContent = this.activeContent.replace(
new RegExp(`
*$`),
`
`
)),
this.select
? this.activeEditor.replaceSelection(this.activeContent)
: this.activeEditor.setValue(this.activeContent),
this.activeEditor.setCursor(t);
}
};
async function g(a) {
let t = [
"\u5BF9\u6807\u9898\u8FDB\u884C\u7F16\u53F7\u683C\u5F0F\u5316",
"\u6E05\u9664\u6240\u6709\u6807\u9898\u7684\u7F16\u53F7",
],
i = [
"\u8BBE\u7F6E\u6807\u9898\u7F16\u53F7",
"\u53D6\u6D88\u6807\u9898\u7F16\u53F7",
],
e = await a.quickAddApi.suggester(t, i);
if (!e) return;
new f(e).run();
}
module.exports = g;