1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00
joplin/packages/renderer
renovate[bot] 37ad911659
Update dependency @types/node to v18.11.16 (#7466)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2022-12-17 01:53:43 +00:00
..
assets All: Update Mermaid: 9.1.7 to 9.2.2 (#7330) 2022-11-23 23:08:27 +00:00
lib
MdToHtml Desktop: Add PDF full screen viewer (#6821) 2022-09-11 14:58:32 +01:00
tests
Tools
vendor
.gitignore
assetsToHeaders.js
defaultNoteStyle.js Desktop: Better handling of bold text to simplify customisation (#5732) 2021-12-28 09:57:34 +00:00
headerAnchor.ts
highlight.ts Chore: Optimize highlight.js package size 2022-05-26 16:46:56 +01:00
HtmlToHtml.ts
htmlUtils.test.ts Desktop: Security: Fixes #6004: Prevent XSS in Goto Anything 2022-01-15 16:53:24 +00:00
htmlUtils.ts Doc: Allow translating website 2022-11-22 18:17:24 +00:00
index.ts
InMemoryCache.ts
jest.config.js
MarkupToHtml.ts Tools: Add Open Graph tags to website 2022-02-24 19:37:34 +00:00
MdToHtml.ts All: Security: Fix XSS when a specially crafted string is passed to the renderer 2022-11-14 17:16:59 +00:00
noteStyle.ts Desktop: New Embedded Pdf Viewer (#6681) 2022-08-04 10:12:22 +01:00
package.json Update dependency @types/node to v18.11.16 (#7466) 2022-12-17 01:53:43 +00:00
pathUtils.ts
publish.sh Tools: Fixed npm publish 2021-12-27 18:02:18 +01:00
README.md
stringUtils.js
tsconfig.json
urlUtils.js
utils.ts

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

Updating a markdown-it plugin

Whenever updating a Markdown-it plugin, such as Katex or Mermaid, make sure to run npm run buildAssets, which will compile the CSS and JS for use in the Joplin applications.

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',
	},
];