UNPKG

hackmd-to-html-cli

Version:

A node.js CLI tool for converting HackMD markdown to HTML.

67 lines 2.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MarkdownItCheckbox = MarkdownItCheckbox; const token_1 = require("./token"); // modified from // https://github.com/mcecot/markdown-it-checkbox function MarkdownItCheckbox(md) { let lastId = 0; function createTokens(checked, label) { const nodes = []; const id = lastId; lastId++; let token = new token_1.MyToken('checkbox_input', 'input', 0); token.attrs = [['type', 'checkbox'], ['id', id.toString()], ['class', 'task-list-item-checkbox']]; if (checked) { token.attrPush(['checked', 'true']); } nodes.push(token); token = new token_1.MyToken('label_open', 'label', 1); token.attrs = [['for', id.toString()]]; nodes.push(token); token = new token_1.MyToken('text', '', 0); token.content = label; nodes.push(token); nodes.push(new token_1.MyToken('label_close', 'label', -1)); return nodes; } function splitTextToken(token) { const matches = token.content.match(/\[(X|\s|_|-)\]\s(.*)/i); if (matches === null) { return null; } let checked = false; const value = matches[1] || ''; const label = matches[2] || ''; if (value == 'X' || value == 'x') { checked = true; } return createTokens(checked, label); } function checkbox(state) { const blockTokens = state.tokens; const l = blockTokens.length; let k = -1; for (let j = 0; j < l; j++) { if (blockTokens[j].type === 'list_item_open') { k = j; } if (blockTokens[j].type !== 'inline') { continue; } let tokens = blockTokens[j].children; for (let i = tokens.length - 1; i >= 0; i--) { const newTokens = splitTextToken(tokens[i]); if (newTokens !== null) { tokens = md.utils.arrayReplaceAt(tokens, i, newTokens); blockTokens[j].children = tokens; if (k !== -1) { blockTokens[k].attrs = [['class', 'task-list-item']]; } } } } } md.core.ruler.push('checkbox', checkbox); } //# sourceMappingURL=checkbox.js.map