1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-27 10:32:58 +02:00
joplin/ReactNativeClient/lib/joplin-renderer
Laurent Cozic 3d8577a689 Plugins: Added support for content scripts
- For now, supports Markdown-it plugins
- Also fixed slow rendering of notes in some cases
- Simplified how Markdown-It plugins are created and cleaned MdToHtml code

commit 89576de289
Merge: c75aa21f 5292fc14
Author: Laurent Cozic <laurent@cozic.net>
Date:   Wed Oct 21 00:23:00 2020 +0100

    Merge branch 'release-1.3' into plugin_content_scripts

commit c75aa21ffd
Author: Laurent Cozic <laurent@cozic.net>
Date:   Wed Oct 21 00:19:52 2020 +0100

    Fixed tests

commit 075187729d
Author: Laurent Cozic <laurent@cozic.net>
Date:   Wed Oct 21 00:11:53 2020 +0100

    Fixed tests

commit 14696b8c65
Author: Laurent Cozic <laurent@cozic.net>
Date:   Tue Oct 20 23:27:58 2020 +0100

    Fixed slow rendering of note

commit 61c09f5bf8
Author: Laurent Cozic <laurent@cozic.net>
Date:   Tue Oct 20 22:35:21 2020 +0100

    Clean up

commit 9f7ea7d865
Author: Laurent Cozic <laurent@cozic.net>
Date:   Tue Oct 20 20:05:31 2020 +0100

    Updated doc

commit 98bf3bde8d
Author: Laurent Cozic <laurent@cozic.net>
Date:   Tue Oct 20 19:56:34 2020 +0100

    Finished converting plugins

commit fe90d92e01
Author: Laurent Cozic <laurent@cozic.net>
Date:   Tue Oct 20 17:52:02 2020 +0100

    Simplified how Markdown-It plugins are created

commit 47c7b864cb
Author: Laurent Cozic <laurent@cozic.net>
Date:   Mon Oct 19 16:40:11 2020 +0100

    Clean up rules

commit d927a238bb
Author: Laurent Cozic <laurent@cozic.net>
Date:   Mon Oct 19 14:29:40 2020 +0100

    Fixed tests

commit 388a56c5dd
Author: Laurent Cozic <laurent@cozic.net>
Date:   Mon Oct 19 14:00:47 2020 +0100

    Add support for content scripts
2020-10-21 00:23:55 +01:00
..
assets Plugins: Added support for content scripts 2020-10-21 00:23:55 +01:00
MdToHtml Plugins: Added support for content scripts 2020-10-21 00:23:55 +01:00
tests Desktop: WYSIWYG: Preserve HTML code in Markdown when editing from wysiwyg editor 2020-04-10 17:12:41 +00:00
Tools Desktop, Mobile: Fixes #2477: Add new Katex fonts to improve rendering (#2478) 2020-02-11 22:53:12 +00:00
vendor Moved the Joplin renderer back to the main repository to make development easier 2020-01-30 21:05:23 +00:00
.gitignore Moved the Joplin renderer back to the main repository to make development easier 2020-01-30 21:05:23 +00:00
assetsToHeaders.js Tools: Enforce and apply eslint rules prefer-const and no-var 2020-03-13 23:46:14 +00:00
defaultNoteStyle.js All: Refactored themes to allow using the same ones in both desktop and mobile version 2020-06-10 22:08:59 +01:00
HtmlToHtml.js Plugins: Added support for content scripts 2020-10-21 00:23:55 +01:00
htmlUtils.js All: Security: Disallow EMBED tags to prevent XSS vulnerability 2020-09-06 19:29:42 +01:00
index.js Plugins: Added support for content scripts 2020-10-21 00:23:55 +01:00
MarkupToHtml.ts Plugins: Added support for content scripts 2020-10-21 00:23:55 +01:00
MdToHtml.ts Plugins: Added support for content scripts 2020-10-21 00:23:55 +01:00
noteStyle.ts Plugins: Added support for content scripts 2020-10-21 00:23:55 +01:00
package-lock.json Desktop: Fixes #3903: Header links with special characters were no longer working 2020-10-16 22:55:48 +01:00
package.json Desktop: Fixes #3903: Header links with special characters were no longer working 2020-10-16 22:55:48 +01:00
pathUtils.ts Plugins: Added support for content scripts 2020-10-21 00:23:55 +01:00
publish.sh Moved the Joplin renderer back to the main repository to make development easier 2020-01-30 21:05:23 +00:00
README.md Desktop, Mobile: Fixes #2374: Fix rendering of certain letters in Katex. Fixed printing when note contains Katex code 2020-02-08 00:15:56 +00:00
stringUtils.js Desktop: Fixed GotoAnything rendering issue with HTML notes 2020-07-07 19:17:33 +01:00
urlUtils.js Moved the Joplin renderer back to the main repository to make development easier 2020-01-30 21:05:23 +00:00
utils.js Revert "Desktop, Mobile: Add support for media player for video and audio files" 2020-08-18 21:52:00 +01:00

Joplin Renderer

This is the renderer used by Joplin to render notes in Markdown or HTML format.

Installation

npm i -s joplin-renderer

Certain plugins require additional assets like CSS, fonts, etc. These assets are in the /assets directory and should be copied to wherever the application can find them at runtime.

Usage

const { MarkupToHtml } = require('joplin-renderer');

const options = {};

// The notes are rendered using the provided theme. The supported theme properties are in `defaultNoteStyle.js`
// and this is what is used if no theme is provided. A `theme` object can be provided to override default theme
// properties.
const theme = {};

const markdown = "Testing `MarkupToHtml` renderer";

const markupToHtml = new MarkupToHtml(options);
const result = await markupToHtml.render(MarkupToHtml.MARKUP_LANGUAGE_MARKDOWN, markdown, theme, options);

console.info('HTML:', result.html);
console.info('Plugin assets:', result.pluginAssets);

When calling render(), an object with the following properties is returned:

  • html: The rendered HTML code
  • pluginAssets: The assets required by the plugins

The assets need to be loaded by the calling application. For example this is how they are loaded in the Joplin desktop application:

function loadPluginAssets(assets) {
	for (let i = 0; i < assets.length; i++) {
		const asset = assets[i];

		if (asset.mime === 'text/css') {
			const link = document.createElement('link');
			link.rel = 'stylesheet';
			link.href = 'pluginAssets/' + asset.name;
			document.getElementById('joplin-container-styleContainer').appendChild(link);
		}
	}
}

Development

### Adding asset files

A plugin (or rule) can have any number of assets, such as CSS or font files, associated with it. To add an asset to a plugin, follow these steps:

  • Add the file under /assets/PLUGIN_NAME/your-asset-file.css
  • Register this file within the plugin using context.pluginAssets[PLUGIN_NAME] = [{ name: 'your-asset-file.css' }]

See katex.js for an example of how this is done.

Adding inline CSS

A plugin can ask for some CSS to be included inline in the rendered HTML. This is convenient as it means no extra file needs to be packaged. Use this syntax to do this:

context.pluginAssets[PLUGIN_NAME] = [
	{
		inline: true,
		text: ".my-css { background-color: 'green' }",
		mime: 'text/css',
	},
];