@diplodoc/transform
Version:
A simple transformer of text in YFM (Yandex Flavored Markdown) to HTML
174 lines • 6.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkboxReplace = exports.CheckboxTokenType = exports.pattern = void 0;
// eslint-disable-next-line no-useless-escape
exports.pattern = /^\[(X|\s|\_|\-)\]\s(.*)/i;
exports.CheckboxTokenType = {
Checkbox: 'checkbox',
CheckboxOpen: 'checkbox_open',
CheckboxClose: 'checkbox_close',
CheckboxInput: 'checkbox_input',
CheckboxLabel: 'checkbox_label',
CheckboxLabelOpen: 'checkbox_label_open',
CheckboxLabelClose: 'checkbox_label_close',
};
function matchOpenToken(tokens, i) {
var _a;
if (tokens[i].type !== 'paragraph_open' || tokens[i + 1].type !== 'inline') {
return false;
}
const firstInline = (_a = tokens[i + 1].children) === null || _a === void 0 ? void 0 : _a[0];
return (firstInline === null || firstInline === void 0 ? void 0 : firstInline.type) === 'text' && exports.pattern.test(firstInline.content);
}
const checkboxReplace = function (_md, opts) {
let lastId = 0;
const defaults = {
divClass: 'checkbox',
idPrefix: 'checkbox',
disabled: true,
};
const options = Object.assign(defaults, opts);
const createTokens = function (state, checkbox) {
let token;
const nodes = [];
/**
* <div class="checkbox">
*/
token = new state.Token(exports.CheckboxTokenType.CheckboxOpen, 'div', 1);
token.block = true;
token.map = [checkbox.startLine, checkbox.endLine];
token.attrs = [['class', options.divClass]];
nodes.push(token);
/**
* <input type="checkbox" id="checkbox{n}" checked="true">
*/
const id = options.idPrefix + lastId;
lastId += 1;
token = new state.Token(exports.CheckboxTokenType.CheckboxInput, 'input', 0);
token.block = true;
token.map = [checkbox.startLine, checkbox.endLine];
token.attrs = [
['type', 'checkbox'],
['id', id],
];
if (options.disabled) {
token.attrSet('disabled', '');
}
if (checkbox.checked === true) {
token.attrSet('checked', 'true');
}
nodes.push(token);
/**
* <label for="checkbox{n}">
*/
token = new state.Token(exports.CheckboxTokenType.CheckboxLabelOpen, 'label', 1);
token.attrs = [['for', id]];
nodes.push(token);
/**
* content of label tag
*/
token = new state.Token('inline', '', 0);
token.content = checkbox.content;
token.children = checkbox.tokens;
token.map = [checkbox.startLine, checkbox.endLine];
nodes.push(token);
/**
* closing tags
*/
token = new state.Token(exports.CheckboxTokenType.CheckboxLabelClose, 'label', -1);
token.block = true;
token.map = [checkbox.startLine, checkbox.endLine];
nodes.push(token);
token = new state.Token(exports.CheckboxTokenType.CheckboxClose, 'div', -1);
token.block = true;
token.map = [checkbox.startLine, checkbox.endLine];
nodes.push(token);
return nodes;
};
return function (state) {
var _a, _b;
const blockTokens = state.tokens;
for (let i = 0; i < blockTokens.length; i++) {
if (!matchOpenToken(blockTokens, i)) {
continue;
}
const pToken = blockTokens[i];
const startLine = (_b = (_a = pToken.map) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : NaN;
const checkboxes = parseInlineContent(blockTokens[i + 1], startLine);
const checkboxTokens = [];
for (const checkbox of checkboxes) {
const first = checkbox.tokens[0];
// remove checkbox markup [X]␣ at start of text content
first.content = first.content.slice(4);
checkbox.content = checkbox.content.trim().slice(4);
checkboxTokens.push(...createTokens(state, checkbox));
}
// replace paragraph tokens with checkbox tokens
if (checkboxTokens.length > 0) {
blockTokens.splice(i, 3, ...checkboxTokens);
i += checkboxTokens.length - 1;
}
}
};
};
exports.checkboxReplace = checkboxReplace;
function parseInlineContent(inlineToken, startLine) {
const lines = splitInlineTokensByBreaks(inlineToken);
return parseCheckboxesByLines(lines, startLine);
}
function splitInlineTokensByBreaks(inlineToken) {
const tokens = inlineToken.children || [];
const contentLines = inlineToken.content.split('\n');
const lines = [];
let lineIdx = 0;
for (const token of tokens) {
lines[lineIdx] || (lines[lineIdx] = {
tokens: [],
content: contentLines[lineIdx],
});
lines[lineIdx].tokens.push(token);
if (isBreakToken(token)) {
lineIdx += 1;
}
}
return lines;
}
function parseCheckboxesByLines(lines, startLine) {
var _a;
const checkboxes = [];
let checkboxIdx = -1;
for (let lineIdx = 0; lineIdx < lines.length; lineIdx++) {
const line = lines[lineIdx];
const first = line === null || line === void 0 ? void 0 : line.tokens[0];
if (!first) {
continue;
}
let match;
if (first.type === 'text' && (match = first.content.match(exports.pattern))) {
const prevLastToken = (_a = checkboxes[checkboxIdx]) === null || _a === void 0 ? void 0 : _a.tokens.at(-1);
if (prevLastToken && isBreakToken(prevLastToken)) {
// remove hanging line breaks
checkboxes[checkboxIdx].tokens.splice(-1);
}
checkboxIdx += 1;
checkboxes[checkboxIdx] || (checkboxes[checkboxIdx] = {
tokens: [],
content: '',
checked: isChecked(match[1]),
startLine: startLine + lineIdx,
endLine: startLine + lineIdx + 1,
});
}
checkboxes[checkboxIdx].tokens.push(...line.tokens);
checkboxes[checkboxIdx].content += line.content + '\n';
checkboxes[checkboxIdx].endLine = startLine + lineIdx + 1;
}
return checkboxes;
}
function isChecked(value) {
return value === 'X' || value === 'x';
}
function isBreakToken(token) {
return token.type === 'softbreak' || token.type === 'hardbreak';
}
//# sourceMappingURL=checkbox.js.map