hackmd-to-html-cli
Version:
A node.js CLI tool for converting HackMD markdown to HTML.
71 lines • 2.83 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MarkdownItYAMLMetadata = MarkdownItYAMLMetadata;
const yaml_1 = __importDefault(require("yaml"));
// modified from
// https://github.com/flaviotordini/markdown-it-yaml
const tokenType = 'yaml_metadata';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function MarkdownItYAMLMetadata(md, callback) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function getLine(state, line) {
const pos = state.bMarks[line];
const max = state.eMarks[line];
return state.src.substring(pos, max);
}
function rule(state, startLine, endLine, silent) {
if (state.blkIndent !== 0 || state.tShift[startLine] < 0) {
return false;
}
if (startLine !== 0) {
return false;
}
if (getLine(state, startLine) !== '---') {
return false;
}
if (silent)
return true;
let nextLine = startLine + 1;
const dataStart = nextLine;
let dataEnd = dataStart;
while (nextLine < endLine) {
if (state.tShift[nextLine] < 0) {
break;
}
if (getLine(state, nextLine) === '---') {
break;
}
dataEnd = nextLine;
nextLine++;
}
const dataStartPos = state.bMarks[dataStart];
const dataEndPos = state.eMarks[dataEnd];
const token = state.push(tokenType, '', 0);
token.content = state.src.substring(dataStartPos, dataEndPos);
state.line = nextLine + 1;
return true;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any
function renderer(tokens, idx, _options, _evn) {
var _a, _b, _c, _d, _e, _f;
const token = tokens[idx];
if (callback) {
const data = yaml_1.default.parse(token.content);
callback({
title: (_a = data.title) !== null && _a !== void 0 ? _a : '',
lang: (_b = data.lang) !== null && _b !== void 0 ? _b : '',
robots: (_c = data.robots) !== null && _c !== void 0 ? _c : '',
description: (_d = data.description) !== null && _d !== void 0 ? _d : '',
dir: (_e = data.dir) !== null && _e !== void 0 ? _e : '',
image: (_f = data.image) !== null && _f !== void 0 ? _f : ''
});
}
return `<!--yaml\n${token.content}\n-->`;
}
md.block.ruler.before('hr', tokenType, rule);
md.renderer.rules[tokenType] = renderer;
}
//# sourceMappingURL=yamlMetadata.js.map
;