@soyvolon/docsify-enhanced-command-line-blocks
Version:
Docsify plugin to enhance the usage of the Command Line plugin from Prism code highlighting.
79 lines (78 loc) • 2.66 kB
JavaScript
const N = (d) => (h, O) => {
const r = d.debugLevel || 0, p = /\<\!--.*(data-.*)=\"(.*)\".*--\>/gm, v = (e, s) => {
let n = [], g = e.matchAll(p);
for (const o of g) {
o.index, o[0].length;
let t = o[1], l = o[2];
n.push({
key: t,
value: l
});
}
const c = s || [];
for (let o = 0; o < c.length; o++) {
const t = c[o];
n.push({
key: t.key,
value: t.value
});
}
return n;
}, A = (e) => {
let s = {
defaultLang: d.defaultLang || "batch",
defaults: [],
matchLangs: d.matchLangs || [
"batch",
"bash",
"shell",
"powershell"
]
};
const n = /\<\!--.*cmd:(\w*):([A-z-]*)(?:=\"(.*)\")?.*--\>/gm;
let g = e.matchAll(n);
for (const c of g) {
r >= 2 && console.log(JSON.stringify(c));
const o = c[1], t = c[2], l = c[3];
l ? s[o].push({
key: t,
value: l
}) : s[o] = t;
}
return r >= 1 && console.log(`Page Settings: ${JSON.stringify(s)}`), s;
};
h.beforeEach((e, s) => {
const n = A(e), g = /((```)(cmd)).*?```/sg;
let c = 0, o = e.matchAll(g);
for (const t of o) {
r >= 2 && console.log(JSON.stringify(t));
const l = t.index + c, i = t[0].length;
let u = n.defaultLang, a = t[0].slice(0, 3) + u + t[0].slice(t[1].length);
c += a.length - i, r >= 2 && console.log(a), e = e.slice(0, l) + a + e.slice(l + i);
}
r >= 2 && console.log(e), s(e);
}), h.afterEach((e, s) => {
const n = A(e), c = new DOMParser().parseFromString(e, "text/html"), o = 'pre[data-lang="' + n.matchLangs.join('"], pre[data-lang="') + '"]', t = c.querySelectorAll(o);
for (const i of t) {
const u = i.querySelector("code");
let a = u.textContent;
const S = n.defaults, m = v(a), y = d.defaults || [];
let L = m.concat(
S.filter((f) => !m.find((b) => b.key == f.key))
);
L = y.concat(
L.filter((f) => !y.find((b) => b.key == f.key))
), r >= 2 && (console.log("global attrs: " + JSON.stringify(y)), console.log("page attrs: " + JSON.stringify(S)), console.log("local attrs: " + JSON.stringify(m)), console.log("final attrs: " + JSON.stringify(L))), a = a.replace(p, "").trimStart(), u.textContent = a;
for (const f of L)
i.setAttribute(f.key, f.value);
i.classList.add("command-line");
}
e = c.documentElement.outerHTML, r >= 2 && console.log(e), s(e);
});
}, k = window.$docsify || {}, T = {
createCommandLineBlocks: k.createCommandLineBlocks ?? {}
};
k.plugins = [].concat(
k.plugins || [],
N(T.createCommandLineBlocks)
);