UNPKG

@diplodoc/transform

Version:

A simple transformer of text in YFM (Yandex Flavored Markdown) to HTML

89 lines 3.82 kB
"use strict"; /* eslint-disable max-len */ const utils_1 = require("./utils"); const wrapInClipboard = (element, id) => { return ` <div class="yfm-clipboard"> ${element} <button class="yfm-clipboard-button" aria-label="Copy"> <svg width="16" height="16" viewBox="0 0 24 24" class="yfm-clipboard-icon" data-animation="${id}"> <path fill="currentColor" d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z" /> <path stroke="currentColor" fill="transparent" stroke-width="1.5" d="M9.5 13l3 3l5 -5" visibility="hidden" > <animate id="visibileAnimation-${id}" attributeName="visibility" from="hidden" to="visible" dur="0.2s" fill="freeze" begin="" /> <animate id="hideAnimation-${id}" attributeName="visibility" from="visible" to="hidden" dur="1s" begin="visibileAnimation-${id}.end+1" fill="freeze" /> </path> </svg> </button> </div> `; }; function termReplace(str, env, escape) { const regTerms = Object.keys(env.terms) .map((el) => el.slice(1)) .map(escape) .join('|'); const regText = '\\[([^\\[]+)\\](\\(\\*(' + regTerms + ')\\))'; const reg = new RegExp(regText, 'g'); const termCode = str.replace(reg, (_match, p1, _p2, p3) => `<i class="yfm yfm-term_title" term-key=":${p3}" id="${(0, utils_1.generateID)()}">${p1}</i>`); return termCode || str; } function addLineNumbers(code) { const lines = code.split('\n'); const lineCount = lines.length; const maxDigits = String(lineCount).length; // Remove trailing empty line if it exists const hasTrailingNewline = code.endsWith('\n'); const linesToProcess = hasTrailingNewline ? lines.slice(0, -1) : lines; return (linesToProcess .map((line, index) => { const lineNumber = String(index + 1).padStart(maxDigits, ' '); return `<span class="yfm-line-number">${lineNumber}</span>${line}`; }) .join('\n') + (hasTrailingNewline ? '\n' : '')); } const code = (md) => { const superCodeRenderer = md.renderer.rules.fence; md.renderer.rules.fence = function (tokens, idx, options, env, self) { const token = tokens[idx]; const showLineNumbers = token.info.includes('showLineNumbers'); let superCode = superCodeRenderer === null || superCodeRenderer === void 0 ? void 0 : superCodeRenderer(tokens, idx, options, env, self); if (superCode && showLineNumbers) { // Extract the code content from the pre/code tags const codeMatch = superCode.match(/<pre[^>]*><code[^>]*>([\s\S]*?)<\/code><\/pre>/); if (codeMatch) { const codeContent = codeMatch[1]; const codeWithLineNumbers = addLineNumbers(codeContent); superCode = superCode.replace(codeContent, codeWithLineNumbers); } } const superCodeWithTerms = superCode && (env === null || env === void 0 ? void 0 : env.terms) ? termReplace(superCode, env, md.utils.escapeRE) : superCode; return wrapInClipboard(superCodeWithTerms, idx); }; }; module.exports = code; //# sourceMappingURL=code.js.map