import { Renderer } from "marked";
class WxRenderer {
constructor(opts) {
this.opts = opts;
let ENV_STRETCH_IMAGE = true;
let footnotes = [];
let footnoteIndex = 0;
let styleMapping = null;
const CODE_FONT_FAMILY =
"Menlo, Operator Mono, Consolas, Monaco, monospace";
let merge = (base, extend) => Object.assign({}, base, extend);
this.buildTheme = (themeTpl) => {
let mapping = {};
let base = merge(themeTpl.BASE, {
"font-family": this.opts.fonts,
"font-size": this.opts.size,
});
for (let ele in themeTpl.inline) {
if (themeTpl.inline.hasOwnProperty(ele)) {
let style = themeTpl.inline[ele];
mapping[ele] = merge(themeTpl.BASE, style);
}
}
let base_block = merge(base, {});
for (let ele in themeTpl.block) {
if (themeTpl.block.hasOwnProperty(ele)) {
let style = themeTpl.block[ele];
if (ele === "code") {
style["font-family"] = CODE_FONT_FAMILY;
}
mapping[ele] = merge(base_block, style);
}
}
return mapping;
};
let getStyles = (tokenName, addition) => {
let arr = [];
let dict = styleMapping[tokenName];
if (!dict) return "";
for (const key in dict) {
arr.push(key + ":" + dict[key]);
}
return `style="${arr.join(";") + (addition || "")}"`;
};
let addFootnote = (title, link) => {
footnotes.push([++footnoteIndex, title, link]);
return footnoteIndex;
};
this.buildFootnotes = () => {
let footnoteArray = footnotes.map((x) => {
if (x[1] === x[2]) {
return `[${x[0]}]
: ${x[1]}
`;
}
return `[${x[0]}]
${x[1]}: ${x[2]}
`;
});
return `
${footnoteArray.join("\n")}
`; }; this.buildAddition = () => { return ` `; }; this.setOptions = (newOpts) => { this.opts = merge(this.opts, newOpts); }; this.hasFootnotes = () => footnotes.length !== 0; this.getRenderer = (status) => { footnotes = []; footnoteIndex = 0; styleMapping = this.buildTheme(this.opts.theme); let renderer = new Renderer(); renderer.heading = (text, level) => { switch (level) { case 1: return ``); return `
${text}`; }; renderer.code = (text, lang) => { text = text.replace(//g, ">"); const codeLines = text .split("\n") .map( (line) => `
${
line || " "
}
`
);
const codeTheme = "github";
return `
${codeLines.join("")}
${text}
`;
renderer.listitem = (text) =>
`<%s/>${text}`;
renderer.list = (text, ordered, start) => {
text = text.replace(/<\/*p.*?>/g, "");
let segments = text.split(`<%s/>`);
if (!ordered) {
text = segments.join("•");
return `${text}
`; } text = segments[0]; for (let i = 1; i < segments.length; i++) { text = text + i + "." + segments[i]; } return `${text}
`; }; renderer.image = (href, title, text) => { let subText = ""; if (text) { subText = `