@diplodoc/transform
Version:
A simple transformer of text in YFM (Yandex Flavored Markdown) to HTML
64 lines • 2.65 kB
JavaScript
"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.substr(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;
}
const code = (md) => {
const superCodeRenderer = md.renderer.rules.fence;
md.renderer.rules.fence = function (tokens, idx, options, env, self) {
const superCode = superCodeRenderer === null || superCodeRenderer === void 0 ? void 0 : superCodeRenderer(tokens, idx, options, env, self);
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