2019-11-01 17:16:40 +08:00
|
|
|
let WxRenderer = function (opts) {
|
|
|
|
this.opts = opts;
|
|
|
|
let ENV_STRETCH_IMAGE = true;
|
|
|
|
|
|
|
|
let footnotes = [];
|
|
|
|
let footnoteIndex = 0;
|
|
|
|
let styleMapping = null;
|
|
|
|
|
2019-11-10 16:59:23 +08:00
|
|
|
const CODE_FONT_FAMILY = "Menlo, Operator Mono, Consolas, Monaco, monospace";
|
2019-11-01 17:16:40 +08:00
|
|
|
|
2019-11-10 15:52:46 +08:00
|
|
|
let merge = (base, extend) => Object.assign({}, base, extend);
|
2019-11-01 17:16:40 +08:00
|
|
|
|
2019-11-10 15:52:46 +08:00
|
|
|
this.buildTheme = themeTpl => {
|
2019-11-01 17:16:40 +08:00
|
|
|
let mapping = {};
|
|
|
|
let base = merge(themeTpl.BASE, {
|
|
|
|
'font-family': this.opts.fonts,
|
|
|
|
'font-size': this.opts.size
|
|
|
|
});
|
|
|
|
let base_block = merge(base, {});
|
|
|
|
for (let ele in themeTpl.inline) {
|
|
|
|
if (themeTpl.inline.hasOwnProperty(ele)) {
|
|
|
|
let style = themeTpl.inline[ele];
|
|
|
|
if (ele === 'codespan') {
|
|
|
|
style['font-family'] = CODE_FONT_FAMILY;
|
|
|
|
style['white-space'] = 'normal';
|
|
|
|
}
|
2019-11-10 16:59:23 +08:00
|
|
|
mapping[ele] = merge(base, style);
|
2019-11-01 17:16:40 +08:00
|
|
|
}
|
|
|
|
}
|
2019-11-10 15:52:46 +08:00
|
|
|
|
2019-11-01 17:16:40 +08:00
|
|
|
for (let ele in themeTpl.block) {
|
|
|
|
if (themeTpl.block.hasOwnProperty(ele)) {
|
|
|
|
let style = themeTpl.block[ele];
|
|
|
|
if (ele === 'code') {
|
2019-11-10 16:59:23 +08:00
|
|
|
style['font-family'] = CODE_FONT_FAMILY;
|
2019-11-01 17:16:40 +08:00
|
|
|
}
|
2019-11-10 16:59:23 +08:00
|
|
|
mapping[ele] = merge(base_block, style);
|
2019-11-01 17:16:40 +08:00
|
|
|
}
|
|
|
|
}
|
2019-11-10 16:59:23 +08:00
|
|
|
return mapping;
|
2019-11-01 17:16:40 +08:00
|
|
|
};
|
|
|
|
|
2019-11-10 15:52:46 +08:00
|
|
|
let getStyles = (tokenName, addition) => {
|
2019-11-01 17:16:40 +08:00
|
|
|
let arr = [];
|
|
|
|
let dict = styleMapping[tokenName];
|
|
|
|
if (!dict) return '';
|
|
|
|
for (const key in dict) {
|
2019-11-10 16:59:23 +08:00
|
|
|
arr.push(key + ':' + dict[key]);
|
2019-11-01 17:16:40 +08:00
|
|
|
}
|
2019-11-10 16:59:23 +08:00
|
|
|
return `style="${arr.join(';') + (addition || '')}"`;
|
2019-11-01 17:16:40 +08:00
|
|
|
};
|
|
|
|
|
2019-11-10 15:52:46 +08:00
|
|
|
let addFootnote = (title, link) => {
|
|
|
|
footnotes.push([++footnoteIndex, title, link]);
|
|
|
|
return footnoteIndex;
|
2019-11-01 17:16:40 +08:00
|
|
|
};
|
|
|
|
|
2019-11-10 15:52:46 +08:00
|
|
|
this.buildFootnotes = () => {
|
|
|
|
let footnoteArray = footnotes.map(x => {
|
2019-11-01 17:16:40 +08:00
|
|
|
if (x[1] === x[2]) {
|
2019-11-10 16:59:23 +08:00
|
|
|
return `<code style="font-size: 90%; opacity: 0.6;">[${x[0]}]</code>: <i>${x[1]}</i><br/>`;
|
2019-11-01 17:16:40 +08:00
|
|
|
}
|
2019-11-10 16:59:23 +08:00
|
|
|
return `<code style="font-size: 90%; opacity: 0.6;">[${x[0]}]</code> ${x[1]}: <i>${x[2]}</i><br/>`;
|
2019-11-01 17:16:40 +08:00
|
|
|
});
|
2019-11-10 16:59:23 +08:00
|
|
|
return `<h4 ${getStyles('h4')}>引用链接</h4><p ${getStyles('footnotes')}>${footnoteArray.join('\n')}</p>`;
|
2019-11-01 17:16:40 +08:00
|
|
|
};
|
|
|
|
|
2019-11-10 15:52:46 +08:00
|
|
|
this.buildAddition = () => {
|
|
|
|
return `
|
|
|
|
<style>
|
|
|
|
.preview-wrapper pre::before {
|
|
|
|
font-family: "SourceSansPro", "HelveticaNeue", Arial, sans-serif;
|
|
|
|
position: absolute;
|
|
|
|
top: 0;
|
|
|
|
right: 0;
|
|
|
|
color: #ccc;
|
|
|
|
text-align: center;
|
|
|
|
font-size: 0.8em;
|
|
|
|
padding: 5px 10px 0;
|
|
|
|
line-height: 15px;
|
|
|
|
height: 15px;
|
|
|
|
font-weight: 600;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
`;
|
|
|
|
}
|
2019-11-01 17:16:40 +08:00
|
|
|
|
2019-11-10 15:52:46 +08:00
|
|
|
this.setOptions = newOpts => {
|
2019-11-10 16:59:23 +08:00
|
|
|
this.opts = merge(this.opts, newOpts);
|
2019-11-01 17:16:40 +08:00
|
|
|
};
|
|
|
|
|
2019-11-10 15:52:46 +08:00
|
|
|
this.hasFootnotes = () => footnotes.length !== 0;
|
2019-11-01 17:16:40 +08:00
|
|
|
|
2019-12-07 21:14:02 +08:00
|
|
|
this.getRenderer = (status) => {
|
2019-11-01 17:16:40 +08:00
|
|
|
footnotes = [];
|
|
|
|
footnoteIndex = 0;
|
|
|
|
|
|
|
|
styleMapping = this.buildTheme(this.opts.theme);
|
|
|
|
let renderer = new marked.Renderer();
|
|
|
|
|
2019-11-10 15:52:46 +08:00
|
|
|
renderer.heading = (text, level) => {
|
2019-11-01 17:16:40 +08:00
|
|
|
switch (level) {
|
|
|
|
case 1:
|
2019-11-07 14:06:59 +08:00
|
|
|
return `<h1 ${getStyles('h1')}>${text}</h1>`;
|
2019-11-01 17:16:40 +08:00
|
|
|
case 2:
|
2019-11-07 14:06:59 +08:00
|
|
|
return `<h2 ${getStyles('h2')}>${text}</h2>`;
|
2019-11-01 17:16:40 +08:00
|
|
|
case 3:
|
2019-11-07 14:06:59 +08:00
|
|
|
return `<h3 ${getStyles('h3')}>${text}</h3>`;
|
2019-11-01 17:16:40 +08:00
|
|
|
default:
|
2019-11-07 14:06:59 +08:00
|
|
|
return `<h4 ${getStyles('h4')}>${text}</h4>`;
|
2019-11-01 17:16:40 +08:00
|
|
|
}
|
|
|
|
};
|
2019-11-10 15:52:46 +08:00
|
|
|
renderer.paragraph = text => `<p ${getStyles('p')}>${text}</p>`;
|
|
|
|
|
|
|
|
renderer.blockquote = text => {
|
2019-11-07 14:06:59 +08:00
|
|
|
text = text.replace(/<p.*?>/, `<p ${getStyles('blockquote_p')}>`);
|
2019-11-10 16:59:23 +08:00
|
|
|
return `<blockquote ${getStyles('blockquote')}>${text}</blockquote>`;
|
2019-11-01 17:16:40 +08:00
|
|
|
};
|
2019-11-10 15:52:46 +08:00
|
|
|
renderer.code = (text, infoString) => {
|
2019-11-01 17:16:40 +08:00
|
|
|
text = text.replace(/</g, "<");
|
|
|
|
text = text.replace(/>/g, ">");
|
|
|
|
|
|
|
|
let lines = text.split('\n');
|
|
|
|
let codeLines = [];
|
|
|
|
let numbers = [];
|
2019-11-10 15:52:46 +08:00
|
|
|
|
2019-11-01 17:16:40 +08:00
|
|
|
for (let i = 0; i < lines.length; i++) {
|
|
|
|
const line = lines[i];
|
2019-11-07 14:06:59 +08:00
|
|
|
codeLines.push(`<code class="prettyprint"><span class="code-snippet_outer">${(line || '<br>')}</span></code>`);
|
2019-11-10 16:59:23 +08:00
|
|
|
numbers.push('<li></li>');
|
2019-11-01 17:16:40 +08:00
|
|
|
}
|
|
|
|
let lang = infoString || '';
|
2019-11-10 15:52:46 +08:00
|
|
|
|
|
|
|
return `
|
|
|
|
<section class="code-snippet__fix code-snippet__js">
|
|
|
|
<ul class="code-snippet__line-index code-snippet__js">${numbers.join('')}</ul>
|
|
|
|
<pre class="code-snippet__js" data-lang="${lang}">
|
|
|
|
${codeLines.join('')}
|
|
|
|
</pre>
|
|
|
|
</section>
|
|
|
|
`;
|
2019-11-01 17:16:40 +08:00
|
|
|
};
|
2019-11-10 15:52:46 +08:00
|
|
|
renderer.codespan = (text, infoString) => `<code ${getStyles('codespan')}>${text}</code>`;
|
|
|
|
renderer.listitem = text => `<span ${getStyles('listitem')}><span style="margin-right: 10px;"><%s/></span>${text}</span>`;
|
|
|
|
|
|
|
|
renderer.list = (text, ordered, start) => {
|
2019-11-01 17:16:40 +08:00
|
|
|
text = text.replace(/<\/*p.*?>/g, '');
|
|
|
|
let segments = text.split(`<%s/>`);
|
|
|
|
if (!ordered) {
|
|
|
|
text = segments.join('•');
|
2019-11-07 14:06:59 +08:00
|
|
|
return `<p ${getStyles('ul')}>${text}</p>`;
|
2019-11-01 17:16:40 +08:00
|
|
|
}
|
|
|
|
text = segments[0];
|
|
|
|
for (let i = 1; i < segments.length; i++) {
|
|
|
|
text = text + i + '.' + segments[i];
|
|
|
|
}
|
2019-11-07 14:06:59 +08:00
|
|
|
return `<p ${getStyles('ol')}>${text}</p>`;
|
2019-11-01 17:16:40 +08:00
|
|
|
};
|
2019-11-10 15:52:46 +08:00
|
|
|
renderer.image = (href, title, text) => {
|
2019-11-01 17:16:40 +08:00
|
|
|
let subText = '';
|
|
|
|
if (text) {
|
2019-11-10 16:59:23 +08:00
|
|
|
subText = `<figcaption ${getStyles('figcaption')}>${text}</figcaption>`;
|
2019-11-01 17:16:40 +08:00
|
|
|
}
|
|
|
|
let figureStyles = getStyles('figure');
|
|
|
|
let imgStyles = getStyles(ENV_STRETCH_IMAGE ? 'image' : 'image_org');
|
2019-11-10 16:59:23 +08:00
|
|
|
return `<figure ${figureStyles}><img ${imgStyles} src="${href}" title="${title}" alt="${text}"/>${subText}</figure>`;
|
2019-11-01 17:16:40 +08:00
|
|
|
};
|
2019-11-10 15:52:46 +08:00
|
|
|
renderer.link = (href, title, text) => {
|
2019-11-01 17:16:40 +08:00
|
|
|
if (href.indexOf('https://mp.weixin.qq.com') === 0) {
|
2019-11-07 14:06:59 +08:00
|
|
|
return `<a href="${href}" title="${(title || text)}" ${getStyles('wx_link')}>${text}</a>`;
|
2019-11-01 17:16:40 +08:00
|
|
|
} else if (href === text) {
|
|
|
|
return text;
|
|
|
|
} else {
|
2019-12-07 21:14:02 +08:00
|
|
|
if (status) {
|
2019-11-01 17:16:40 +08:00
|
|
|
let ref = addFootnote(title || text, href);
|
2019-11-07 14:06:59 +08:00
|
|
|
return `<span ${getStyles('link')}>${text}<sup>[${ref}]</sup></span>`;
|
2019-11-01 17:16:40 +08:00
|
|
|
} else {
|
2019-12-07 21:14:02 +08:00
|
|
|
return text;
|
2019-11-01 17:16:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-11-10 15:52:46 +08:00
|
|
|
renderer.strong = text => `<strong ${getStyles('strong')}>${text}</strong>`;
|
|
|
|
renderer.em = text => `<p ${getStyles('p', ';font-style: italic;')}>${text}</p>`;
|
2019-12-26 22:30:20 +08:00
|
|
|
renderer.table = (header, body) => `<section style="padding:0 8px;"><table class="preview-table"><thead ${getStyles('thead')}>${header}</thead><tbody>${body}</tbody></table></section>`;
|
|
|
|
// renderer.tablerow = (text) => `<tr style="">${text}</tr>`;
|
2019-11-10 15:52:46 +08:00
|
|
|
renderer.tablecell = (text, flags) => `<td ${getStyles('td')}>${text}</td>`;
|
|
|
|
renderer.hr = () => `<hr style="border-style: solid;border-width: 1px 0 0;border-color: rgba(0,0,0,0.1);-webkit-transform-origin: 0 0;-webkit-transform: scale(1, 0.5);transform-origin: 0 0;transform: scale(1, 0.5);">`;
|
2019-11-10 16:59:23 +08:00
|
|
|
return renderer;
|
2019-11-01 17:16:40 +08:00
|
|
|
}
|
|
|
|
};
|