-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
30 lines (26 loc) · 994 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import Typograf from "typograf";
/**
* @typedef {import('typograf').TypografPrefs} TypografPrefs
*
* @typedef {object} PluginOptions
* @property {TypografPrefs} [typografOptions] - Typograf options, defaults to `{ locale: 'ru' }`.
* @property {(tp: Typograf) => void} [typografSetup] - Function that allows to customize typograf programmatically.
*/
/** @type {import('markdown-it').PluginWithOptions<PluginOptions>} */
export default function (md, opts = {}) {
const tp = new Typograf(opts.typografOptions || { locale: "ru" });
if (opts.typografSetup) opts.typografSetup(tp);
/** @type {(token: import('markdown-it/lib/token.mjs').default) => void} */
function execute(token) {
if (token.children) {
token.children.forEach(execute);
} else if (token.content) {
// console.debug(token)
token.content = tp.execute(token.content);
}
}
md.core.ruler.push("typograf", function ({ tokens }) {
tokens.forEach(execute);
return true;
});
}