@diplodoc/transform
Version:
A simple transformer of text in YFM (Yandex Flavored Markdown) to HTML
53 lines • 1.55 kB
JavaScript
;
const js_yaml_1 = require("js-yaml");
/* eslint-disable @typescript-eslint/no-explicit-any */
const SEP = '---';
const meta = (md) => {
const mdAny = md;
mdAny.meta = mdAny.meta || {};
const getLine = (state, line) => state.src.slice(state.bMarks[line], state.eMarks[line]);
md.block.ruler.before('code', 'meta', (state, startLine, endLine) => {
if (startLine !== 0 || state.blkIndent !== 0) {
return false;
}
if (state.tShift[startLine] < 0) {
return false;
}
if (getLine(state, startLine).trim() !== SEP) {
return false;
}
let line = startLine + 1;
const lines = [];
let found = false;
while (line < endLine) {
const str = getLine(state, line);
if (str.trim() === SEP) {
found = true;
break;
}
if (state.tShift[line] < 0) {
return false;
}
lines.push(str);
line++;
}
if (!found) {
return false;
}
let data;
try {
data = (0, js_yaml_1.load)(lines.join('\n')) || {};
}
catch (_a) {
return false;
}
if (typeof data !== 'object' || data === null || Array.isArray(data)) {
return false;
}
mdAny.meta = data;
state.line = line + 1;
return true;
}, { alt: [] });
};
module.exports = meta;
//# sourceMappingURL=meta.js.map