mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-18 09:35:20 +02:00
cb3e1cf1e9
commit2fb6cee901
Merge:4e303be85f
db509955f6
Author: Laurent Cozic <laurent@cozic.net> Date: Fri Oct 16 16:24:07 2020 +0100 Merge branch 'dev' into rn_63 commit4e303be85f
Author: Laurent Cozic <laurent@cozic.net> Date: Fri Oct 16 16:14:39 2020 +0100 Clean up commite3a37ec2d6
Author: Laurent Cozic <laurent@cozic.net> Date: Fri Oct 16 15:57:55 2020 +0100 Use different script for pre-commit and manual start commitbd236648fc
Author: Laurent Cozic <laurent@cozic.net> Date: Fri Oct 16 15:56:45 2020 +0100 Removed RN eslint config commite7feda41c9
Author: Laurent Cozic <laurent@cozic.net> Date: Fri Oct 16 15:27:08 2020 +0100 Revert "Disable git hook for now" This reverts commit89263ac742
. commitcfd63fe46f
Author: Laurent Cozic <laurent@cozic.net> Date: Fri Oct 16 13:02:32 2020 +0100 Ask permission to use geo-location commit66059939a3
Author: Laurent Cozic <laurent@cozic.net> Date: Fri Oct 16 12:26:20 2020 +0100 Fixed WebView race condition commit1e0d2b7b86
Author: Laurent Cozic <laurent@cozic.net> Date: Fri Oct 16 11:56:21 2020 +0100 Fixed webview issues commitf537d22d7f
Author: Laurent Cozic <laurent@cozic.net> Date: Fri Oct 16 11:08:29 2020 +0100 Improve resource file watching commiteec32cf70a
Author: Laurent Cozic <laurent@cozic.net> Date: Thu Oct 15 18:40:13 2020 +0100 Removed cache package dependency and implemented one more suitable for React Native commitefa346fea4
Author: Laurent Cozic <laurent@cozic.net> Date: Thu Oct 15 14:57:21 2020 +0100 iOS: Added fonts to Info.plist although it was working without it commit572b647bc0
Author: Laurent Cozic <laurent@cozic.net> Date: Thu Oct 15 14:56:49 2020 +0100 Specify content-type header for OneDrive to prevent network error commitbcedf6c7f0
Author: Laurent Cozic <laurent@cozic.net> Date: Thu Oct 15 12:45:01 2020 +0100 iOS: Disable long press menu since it is already built-in commit7359dd61d1
Author: Laurent Cozic <laurent@cozic.net> Date: Thu Oct 15 12:37:40 2020 +0100 Removed unused react-native-device-info commit2d63ab36d3
Author: Laurent Cozic <laurent@cozic.net> Date: Thu Oct 15 12:35:54 2020 +0100 iOS: Fixed taking a picture commit8e2875a91c
Author: Laurent Cozic <laurent@cozic.net> Date: Thu Oct 15 12:11:13 2020 +0100 iOS: Restored camera roll functionality commit75f5edf2ad
Author: Laurent Cozic <laurent@cozic.net> Date: Thu Oct 15 11:40:13 2020 +0100 iOS: Fixed build settings commitb220c98419
Author: Laurent Cozic <laurent@cozic.net> Date: Thu Oct 15 11:40:03 2020 +0100 iOS: Got images to work with WebKit commitc34b43e841
Author: Laurent Cozic <laurent@cozic.net> Date: Thu Oct 15 10:24:52 2020 +0100 iOS: Restore more settings commit32997611e6
Author: Laurent Cozic <laurent@cozic.net> Date: Thu Oct 15 10:15:14 2020 +0100 iOS: Added back icons and other properties commitb5811d7f7c
Author: Laurent Cozic <laurent@cozic.net> Date: Wed Oct 14 23:53:14 2020 +0100 Got iOS build to work commitdc6d7c00e0
Author: Laurent Cozic <laurent@cozic.net> Date: Wed Oct 14 18:40:06 2020 +0100 Imported old settings in gradle build commitdff59f5603
Author: Laurent Cozic <laurent@cozic.net> Date: Wed Oct 14 18:20:00 2020 +0100 Restored sharing commit0bdb449e72
Author: Laurent Cozic <laurent@cozic.net> Date: Wed Oct 14 17:25:40 2020 +0100 Updated NoteBodyViewer commit0c0d228815
Author: Laurent Cozic <laurent@cozic.net> Date: Wed Oct 14 16:54:42 2020 +0100 Fixed networking commit6ff45ce485
Author: Laurent Cozic <laurent@cozic.net> Date: Wed Oct 14 13:11:00 2020 +0100 Fixed document picker commitcc889182b6
Author: Laurent Cozic <laurent@cozic.net> Date: Wed Oct 14 12:56:27 2020 +0100 Added back support for alarms commit040261abfa
Author: Laurent Cozic <laurent@cozic.net> Date: Tue Oct 13 22:04:49 2020 +0100 Fixed Clipboard and remove image-picker package commit1077ad8f16
Author: Laurent Cozic <laurent@cozic.net> Date: Tue Oct 13 21:54:52 2020 +0100 Fixed Select Alarm dialog and PoorManIntervals class commit8296676fd5
Author: Laurent Cozic <laurent@cozic.net> Date: Tue Oct 13 21:32:52 2020 +0100 Fixed icons and warnings commit3b0e3f6f43
Author: Laurent Cozic <laurent@cozic.net> Date: Tue Oct 13 17:02:59 2020 +0100 Got app to build again commit89263ac742
Author: Laurent Cozic <laurent@cozic.net> Date: Tue Oct 13 15:41:17 2020 +0100 Disable git hook for now commitd6da162f67
Author: Laurent Cozic <laurent@cozic.net> Date: Tue Oct 13 15:39:12 2020 +0100 Restored back all RN packages commit7f8ce3732c
Author: Laurent Cozic <laurent@cozic.net> Date: Tue Oct 13 15:13:12 2020 +0100 Restored base packages commitea59726eb3
Author: Laurent Cozic <laurent@cozic.net> Date: Tue Oct 13 15:05:17 2020 +0100 Started over from scratch
363 lines
12 KiB
JavaScript
363 lines
12 KiB
JavaScript
const MarkdownIt = require('markdown-it');
|
|
const md5 = require('md5');
|
|
const noteStyle = require('./noteStyle');
|
|
const { fileExtension } = require('./pathUtils');
|
|
const InMemoryCache = require('lib/InMemoryCache').default;
|
|
|
|
// /!\/!\ Note: the order of rules is important!! /!\/!\
|
|
const rules = {
|
|
fence: require('./MdToHtml/rules/fence').default,
|
|
sanitize_html: require('./MdToHtml/rules/sanitize_html').default,
|
|
image: require('./MdToHtml/rules/image'),
|
|
checkbox: require('./MdToHtml/rules/checkbox').default,
|
|
katex: require('./MdToHtml/rules/katex'),
|
|
link_open: require('./MdToHtml/rules/link_open'),
|
|
html_image: require('./MdToHtml/rules/html_image'),
|
|
highlight_keywords: require('./MdToHtml/rules/highlight_keywords'),
|
|
code_inline: require('./MdToHtml/rules/code_inline'),
|
|
fountain: require('./MdToHtml/rules/fountain'),
|
|
mermaid: require('./MdToHtml/rules/mermaid').default,
|
|
};
|
|
|
|
// const eventManager = require('lib/eventManager').default;
|
|
const setupLinkify = require('./MdToHtml/setupLinkify');
|
|
const hljs = require('highlight.js');
|
|
const nodeSlug = require('slug');
|
|
const markdownItAnchor = require('markdown-it-anchor');
|
|
// The keys must match the corresponding entry in Setting.js
|
|
const plugins = {
|
|
mark: { module: require('markdown-it-mark') },
|
|
footnote: { module: require('markdown-it-footnote') },
|
|
sub: { module: require('markdown-it-sub') },
|
|
sup: { module: require('markdown-it-sup') },
|
|
deflist: { module: require('markdown-it-deflist') },
|
|
abbr: { module: require('markdown-it-abbr') },
|
|
emoji: { module: require('markdown-it-emoji') },
|
|
insert: { module: require('markdown-it-ins') },
|
|
multitable: { module: require('markdown-it-multimd-table'), options: { multiline: true, rowspan: true, headerless: true } },
|
|
toc: { module: require('markdown-it-toc-done-right'), options: { listType: 'ul', slugify: slugify } },
|
|
expand_tabs: { module: require('markdown-it-expand-tabs'), options: { tabWidth: 4 } },
|
|
};
|
|
const defaultNoteStyle = require('./defaultNoteStyle');
|
|
|
|
function slugify(s) {
|
|
return nodeSlug(s);
|
|
}
|
|
|
|
// Share across all instances of MdToHtml
|
|
const inMemoryCache = new InMemoryCache(20);
|
|
|
|
class MdToHtml {
|
|
constructor(options = null) {
|
|
if (!options) options = {};
|
|
|
|
// Must include last "/"
|
|
this.resourceBaseUrl_ = 'resourceBaseUrl' in options ? options.resourceBaseUrl : null;
|
|
|
|
this.cachedOutputs_ = {};
|
|
|
|
this.lastCodeHighlightCacheKey_ = null;
|
|
this.cachedHighlightedCode_ = {};
|
|
this.ResourceModel_ = options.ResourceModel;
|
|
this.pluginOptions_ = options.pluginOptions ? options.pluginOptions : {};
|
|
this.contextCache_ = inMemoryCache;
|
|
|
|
this.tempDir_ = options.tempDir;
|
|
this.fsDriver_ = {
|
|
writeFile: (/* path, content, encoding = 'base64'*/) => { throw new Error('writeFile not set'); },
|
|
exists: (/* path*/) => { throw new Error('exists not set'); },
|
|
cacheCssToFile: (/* cssStrings*/) => { throw new Error('cacheCssToFile not set'); },
|
|
};
|
|
|
|
if (options.fsDriver) {
|
|
if (options.fsDriver.writeFile) this.fsDriver_.writeFile = options.fsDriver.writeFile;
|
|
if (options.fsDriver.exists) this.fsDriver_.exists = options.fsDriver.exists;
|
|
if (options.fsDriver.cacheCssToFile) this.fsDriver_.cacheCssToFile = options.fsDriver.cacheCssToFile;
|
|
}
|
|
}
|
|
|
|
fsDriver() {
|
|
return this.fsDriver_;
|
|
}
|
|
|
|
tempDir() {
|
|
return this.tempDir_;
|
|
}
|
|
|
|
static pluginNames() {
|
|
const output = [];
|
|
for (const n in rules) output.push(n);
|
|
for (const n in plugins) output.push(n);
|
|
return output;
|
|
}
|
|
|
|
pluginOptions(name) {
|
|
let o = this.pluginOptions_[name] ? this.pluginOptions_[name] : {};
|
|
o = Object.assign({
|
|
enabled: true,
|
|
}, o);
|
|
return o;
|
|
}
|
|
|
|
pluginEnabled(name) {
|
|
return this.pluginOptions(name).enabled;
|
|
}
|
|
|
|
processPluginAssets(pluginAssets) {
|
|
const files = [];
|
|
const cssStrings = [];
|
|
for (const pluginName in pluginAssets) {
|
|
for (const asset of pluginAssets[pluginName]) {
|
|
let mime = asset.mime;
|
|
|
|
if (!mime && asset.inline) throw new Error('Mime type is required for inline assets');
|
|
|
|
if (!mime) {
|
|
const ext = fileExtension(asset.name).toLowerCase();
|
|
// For now it's only useful to support CSS and JS because that's what needs to be added
|
|
// by the caller with <script> or <style> tags. Everything, like fonts, etc. is loaded
|
|
// via CSS or some other ways.
|
|
mime = 'application/octet-stream';
|
|
if (ext === 'css') mime = 'text/css';
|
|
if (ext === 'js') mime = 'application/javascript';
|
|
}
|
|
|
|
if (asset.inline) {
|
|
if (mime === 'text/css') {
|
|
cssStrings.push(asset.text);
|
|
} else {
|
|
throw new Error(`Unsupported inline mime type: ${mime}`);
|
|
}
|
|
} else {
|
|
const name = `${pluginName}/${asset.name}`;
|
|
files.push(Object.assign({}, asset, {
|
|
name: name,
|
|
path: `pluginAssets/${name}`,
|
|
mime: mime,
|
|
}));
|
|
}
|
|
}
|
|
}
|
|
|
|
return {
|
|
pluginAssets: files,
|
|
cssStrings: cssStrings,
|
|
};
|
|
}
|
|
|
|
async allAssets(theme) {
|
|
const assets = {};
|
|
for (const key in rules) {
|
|
if (!this.pluginEnabled(key)) continue;
|
|
const rule = rules[key];
|
|
|
|
if (rule.style) {
|
|
assets[key] = rule.style(theme);
|
|
}
|
|
}
|
|
|
|
const processedAssets = this.processPluginAssets(assets);
|
|
processedAssets.cssStrings.splice(0, 0, noteStyle(theme));
|
|
const output = await this.outputAssetsToExternalAssets_(processedAssets);
|
|
return output.pluginAssets;
|
|
}
|
|
|
|
async outputAssetsToExternalAssets_(output) {
|
|
for (const cssString of output.cssStrings) {
|
|
output.pluginAssets.push(await this.fsDriver().cacheCssToFile(cssString));
|
|
}
|
|
delete output.cssStrings;
|
|
return output;
|
|
}
|
|
|
|
removeMarkdownItWrappingParagraph_(html) {
|
|
// <p></p>\n
|
|
if (html.length < 8) return html;
|
|
if (html.substr(0, 3) !== '<p>') return html;
|
|
if (html.slice(-5) !== '</p>\n') return html;
|
|
return html.substring(3, html.length - 5);
|
|
}
|
|
|
|
clearCache() {
|
|
this.cachedOutputs_ = {};
|
|
}
|
|
|
|
// "theme" is the theme as returned by themeStyle()
|
|
async render(body, theme = null, options = null) {
|
|
options = Object.assign({}, {
|
|
// In bodyOnly mode, the rendered Markdown is returned without the wrapper DIV
|
|
bodyOnly: false,
|
|
// In splitted mode, the CSS and HTML will be returned in separate properties.
|
|
// In non-splitted mode, CSS and HTML will be merged in the same document.
|
|
splitted: false,
|
|
// When this is true, all assets such as CSS or JS are returned as external
|
|
// files. Otherwise some of them might be in the cssStrings property.
|
|
externalAssetsOnly: false,
|
|
postMessageSyntax: 'postMessage',
|
|
highlightedKeywords: [],
|
|
codeTheme: 'atom-one-light.css',
|
|
theme: Object.assign({}, defaultNoteStyle, theme),
|
|
plugins: {},
|
|
}, options);
|
|
|
|
// The "codeHighlightCacheKey" option indicates what set of cached object should be
|
|
// associated with this particular Markdown body. It is only used to allow us to
|
|
// clear the cache whenever switching to a different note.
|
|
// If "codeHighlightCacheKey" is not specified, code highlighting won't be cached.
|
|
if (options.codeHighlightCacheKey !== this.lastCodeHighlightCacheKey_ || !options.codeHighlightCacheKey) {
|
|
this.cachedHighlightedCode_ = {};
|
|
this.lastCodeHighlightCacheKey_ = options.codeHighlightCacheKey;
|
|
}
|
|
|
|
const cacheKey = md5(escape(body + JSON.stringify(options) + JSON.stringify(options.theme)));
|
|
const cachedOutput = this.cachedOutputs_[cacheKey];
|
|
if (cachedOutput) return cachedOutput;
|
|
|
|
const context = {
|
|
css: {},
|
|
pluginAssets: {},
|
|
cache: this.contextCache_,
|
|
};
|
|
|
|
const ruleOptions = Object.assign({}, options, {
|
|
resourceBaseUrl: this.resourceBaseUrl_,
|
|
ResourceModel: this.ResourceModel_,
|
|
});
|
|
|
|
const markdownIt = new MarkdownIt({
|
|
breaks: !this.pluginEnabled('softbreaks'),
|
|
typographer: this.pluginEnabled('typographer'),
|
|
linkify: true,
|
|
html: true,
|
|
highlight: (str, lang) => {
|
|
let outputCodeHtml = '';
|
|
|
|
// The strings includes the last \n that is part of the fence,
|
|
// so we remove it because we need the exact code in the source block
|
|
const trimmedStr = str.replace(/(.*)\n$/, '$1');
|
|
const sourceBlockHtml = `<pre class="joplin-source" data-joplin-language="${lang}" data-joplin-source-open="\`\`\`${lang} " data-joplin-source-close=" \`\`\`">${markdownIt.utils.escapeHtml(trimmedStr)}</pre>`;
|
|
|
|
try {
|
|
let hlCode = '';
|
|
|
|
const cacheKey = md5(`${str}_${lang}`);
|
|
|
|
if (options.codeHighlightCacheKey && this.cachedHighlightedCode_[cacheKey]) {
|
|
hlCode = this.cachedHighlightedCode_[cacheKey];
|
|
} else {
|
|
if (lang && hljs.getLanguage(lang)) {
|
|
hlCode = hljs.highlight(lang, trimmedStr, true).value;
|
|
} else {
|
|
hlCode = hljs.highlightAuto(trimmedStr).value;
|
|
}
|
|
this.cachedHighlightedCode_[cacheKey] = hlCode;
|
|
}
|
|
|
|
context.pluginAssets['highlight.js'] = [
|
|
{ name: options.codeTheme },
|
|
];
|
|
|
|
outputCodeHtml = hlCode;
|
|
} catch (error) {
|
|
outputCodeHtml = markdownIt.utils.escapeHtml(trimmedStr);
|
|
}
|
|
|
|
return {
|
|
wrapCode: false,
|
|
html: `<div class="joplin-editable">${sourceBlockHtml}<pre class="hljs"><code>${outputCodeHtml}</code></pre></div>`,
|
|
};
|
|
},
|
|
});
|
|
|
|
// To add a plugin, there are three options:
|
|
//
|
|
// 1. If the plugin does not need any application specific data, use the standard way:
|
|
//
|
|
// const someMarkdownPlugin = require('someMarkdownPlugin');
|
|
// markdownIt.use(someMarkdownPlugin);
|
|
//
|
|
// 2. If the plugin does not need any application specific data, and you want the user
|
|
// to be able to toggle the plugin:
|
|
//
|
|
// Add the plugin to the plugins object
|
|
// const plugins = {
|
|
// plugin: require('someMarkdownPlugin'),
|
|
// }
|
|
//
|
|
// And add a corresponding entry into Setting.js
|
|
// 'markdown.plugin.mark': {value: true, type: Setting.TYPE_BOOL, section: 'plugins', public: true, appTypes: ['mobile', 'desktop'], label: () => _('Enable ==mark== syntax')},
|
|
//
|
|
// 3. If the plugin needs application data (in ruleOptions) or needs to pass data (CSS, files to load, etc.) back
|
|
// to the application (using the context object), use the application-specific way:
|
|
//
|
|
// const imagePlugin = require('./MdToHtml/rules/image');
|
|
// markdownIt.use(imagePlugin(context, ruleOptions));
|
|
//
|
|
// Using the `context` object, a plugin can define what additional assets they need (css, fonts, etc.) using context.pluginAssets.
|
|
// The calling application will need to handle loading these assets.
|
|
|
|
for (const key in rules) {
|
|
if (!this.pluginEnabled(key)) continue;
|
|
const rule = rules[key];
|
|
const ruleInstall = rule.install ? rule.install : rule;
|
|
markdownIt.use(ruleInstall(context, { ...ruleOptions }));
|
|
}
|
|
|
|
markdownIt.use(markdownItAnchor, { slugify: slugify });
|
|
|
|
for (const key in plugins) {
|
|
if (this.pluginEnabled(key)) {
|
|
markdownIt.use(plugins[key].module, plugins[key].options);
|
|
}
|
|
}
|
|
|
|
// const extraPlugins = eventManager.filterEmit('mdToHtmlPlugins', {});
|
|
// for (const key in extraPlugins) {
|
|
// markdownIt.use(extraPlugins[key].module, extraPlugins[key].options);
|
|
// }
|
|
|
|
setupLinkify(markdownIt);
|
|
|
|
const renderedBody = markdownIt.render(body);
|
|
|
|
let cssStrings = noteStyle(options.theme);
|
|
|
|
let output = this.processPluginAssets(context.pluginAssets);
|
|
cssStrings = cssStrings.concat(output.cssStrings);
|
|
|
|
if (options.userCss) cssStrings.push(options.userCss);
|
|
|
|
if (options.bodyOnly) {
|
|
// Markdown-it wraps any content in <p></p> by default. There's a function to parse without
|
|
// adding these tags (https://github.com/markdown-it/markdown-it/issues/540#issuecomment-471123983)
|
|
// however when using it, it seems the loaded plugins are not used. In my tests, just changing
|
|
// render() to renderInline() means the checkboxes would not longer be rendered. So instead
|
|
// of using this function, we manually remove the <p></p> tags.
|
|
output.html = this.removeMarkdownItWrappingParagraph_(renderedBody);
|
|
output.cssStrings = cssStrings;
|
|
} else {
|
|
const styleHtml = `<style>${cssStrings.join('\n')}</style>`;
|
|
output.html = `${styleHtml}<div id="rendered-md">${renderedBody}</div>`;
|
|
|
|
if (options.splitted) {
|
|
output.cssStrings = cssStrings;
|
|
output.html = `<div id="rendered-md">${renderedBody}</div>`;
|
|
}
|
|
}
|
|
|
|
if (options.externalAssetsOnly) output = await this.outputAssetsToExternalAssets_(output);
|
|
|
|
// Fow now, we keep only the last entry in the cache
|
|
this.cachedOutputs_ = {};
|
|
this.cachedOutputs_[cacheKey] = output;
|
|
|
|
return output;
|
|
}
|
|
|
|
injectedJavaScript() {
|
|
return '';
|
|
}
|
|
}
|
|
|
|
module.exports = MdToHtml;
|