Desktop, Mobile: Fixed: Some images were not being displayed

pull/1524/head
Laurent Cozic 2019-05-11 09:49:56 +01:00
parent dccd489fcc
commit 863f5bcf18
3 changed files with 24 additions and 53 deletions

View File

@ -13,8 +13,7 @@ const rules = {
checkbox: require('./MdToHtml/rules/checkbox'),
katex: require('./MdToHtml/rules/katex'),
link_open: require('./MdToHtml/rules/link_open'),
html_block: require('./MdToHtml/rules/html_block'),
html_inline: require('./MdToHtml/rules/html_inline'),
html_image: require('./MdToHtml/rules/html_image'),
highlight_keywords: require('./MdToHtml/rules/highlight_keywords'),
code_inline: require('./MdToHtml/rules/code_inline'),
};
@ -124,7 +123,7 @@ class MdToHtml {
markdownIt.use(rules.image(context, ruleOptions));
markdownIt.use(rules.checkbox(context, ruleOptions));
markdownIt.use(rules.link_open(context, ruleOptions));
markdownIt.use(rules.html_block(context, ruleOptions));
markdownIt.use(rules.html_image(context, ruleOptions));
if (Setting.value('markdown.plugin.katex'))
markdownIt.use(rules.katex(context, ruleOptions));
markdownIt.use(rules.highlight_keywords(context, ruleOptions));

View File

@ -19,23 +19,34 @@ function renderImageHtml(before, src, after, ruleOptions) {
}
function installRule(markdownIt, mdOptions, ruleOptions) {
const defaultRender = markdownIt.renderer.rules.html_block || function(tokens, idx, options, env, self) {
const htmlBlockDefaultRender = markdownIt.renderer.rules.html_block || function(tokens, idx, options, env, self) {
return self.renderToken(tokens, idx, options);
};
const htmlInlineDefaultRender = markdownIt.renderer.rules.html_inline || function(tokens, idx, options, env, self) {
return self.renderToken(tokens, idx, options);
};
const imageRegex = /<img(.*?)src=["'](.*?)["'](.*?)\/>/
markdownIt.renderer.rules.html_block = function(tokens, idx, options, env, self) {
const token = tokens[idx];
const content = token.content;
const handleImageTags = function(defaultRender) {
return function(tokens, idx, options, env, self) {
const token = tokens[idx];
const content = token.content;
if (!content.match(imageRegex)) return defaultRender(tokens, idx, options, env, self);
return content.replace(imageRegex, (v, before, src, after) => {
if (!Resource.isResourceUrl(src)) return defaultRender(tokens, idx, options, env, self);
return renderImageHtml(before, src, after, ruleOptions);
});
};
if (!content.match(imageRegex)) return defaultRender(tokens, idx, options, env, self);
return content.replace(imageRegex, (v, before, src, after) => {
if (!Resource.isResourceUrl(src)) return defaultRender(tokens, idx, options, env, self);
return renderImageHtml(before, src, after, ruleOptions);
});
}
}
// It seems images sometimes are inline, sometimes a block
// to make sure they both render correctly.
markdownIt.renderer.rules.html_block = handleImageTags(htmlBlockDefaultRender);
markdownIt.renderer.rules.html_inline = handleImageTags(htmlInlineDefaultRender);
}
module.exports = function(context, ruleOptions) {

View File

@ -1,39 +0,0 @@
// This rule is no longer needed because HTML anchors (as opposed to those generated from Markdown)
// are handled in webviewLib. Keeping it here for reference.
const Entities = require('html-entities').AllHtmlEntities;
const htmlentities = (new Entities()).encode;
const Resource = require('lib/models/Resource.js');
const utils = require('../utils');
function installRule(markdownIt, mdOptions, ruleOptions) {
const defaultRender = markdownIt.renderer.rules.html_block || function(tokens, idx, options, env, self) {
return self.renderToken(tokens, idx, options);
};
const anchorRegex = /<a (.*)>/
markdownIt.renderer.rules.html_inline = function(tokens, idx, options, env, self) {
const token = tokens[idx];
const content = token.content;
if (!content.match(anchorRegex)) return defaultRender(tokens, idx, options, env, self);
return content.replace(anchorRegex, (v, content) => {
let js = `
var href = this.getAttribute('href');
if (!href || href.indexOf('http') < 0) return true;
` + ruleOptions.postMessageSyntax + `(href);
return false;
`;
js = js.split('\n').join(' ').replace(/\t/g, '');
return '<a onclick="' + js + '" ' + content + '>';
});
};
}
module.exports = function(context, ruleOptions) {
return function(md, mdOptions) {
installRule(md, mdOptions, ruleOptions);
};
};