1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-06-30 23:44:55 +02:00

Syntax highlighting on webview

This commit is contained in:
Laurent Cozic
2017-11-09 23:28:08 +00:00
parent d4a0f7791a
commit fea29b95de
90 changed files with 8305 additions and 9 deletions

@ -146,6 +146,7 @@ class MdToHtml {
let openTag = null;
let closeTag = null;
let attrs = t.attrs ? t.attrs : [];
const isCodeBlock = tag === 'code' && t.block;
if (t.map) attrs.push(['data-map', t.map.join(':')]);
@ -157,17 +158,25 @@ class MdToHtml {
openTag = tag;
} else if (t.type === 'link_open') {
openTag = 'a';
} else if (isCodeBlock) {
openTag = 'pre';
}
if (openTag) {
if (openTag === 'a') {
output.push(this.renderOpenLink_(attrs, options));
} else {
const attrsHtml = attrs ? this.renderAttrs_(attrs) : '';
const attrsHtml = this.renderAttrs_(attrs);
output.push('<' + openTag + (attrsHtml ? ' ' + attrsHtml : '') + '>');
}
}
if (isCodeBlock) {
const codeAttrs = ['code'];
if (t.info) codeAttrs.push(t.info); // t.info contains the language when the token is a codeblock
output.push('<code class="' + codeAttrs.join(' ') + '">');
}
if (t.type === 'image') {
if (t.content) attrs.push(['title', t.content]);
output.push(this.renderImage_(attrs, options));
@ -186,8 +195,12 @@ class MdToHtml {
closeTag = 'a';
} else if (tag && t.type.indexOf('inline') >= 0) {
closeTag = openTag;
} else if (isCodeBlock) {
closeTag = openTag;
}
if (isCodeBlock) output.push('</code>');
if (closeTag) {
if (closeTag === 'a') {
output.push(this.renderCloseLink_(attrs, options));
@ -302,6 +315,7 @@ class MdToHtml {
const styleHtml = '<style>' + normalizeCss + "\n" + css + '</style>';
const output = styleHtml + renderedBody;
this.cachedContent_ = output;
this.cachedContentKey_ = cacheKey;
return this.cachedContent_;