1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00
joplin/packages/renderer
renovate[bot] c1e5adf658
Update dependency @types/node to v18.19.8 (#9866)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-02-07 02:35:16 +00:00
..
assets Desktop,Mobile: Resolves #3201: Render mermaid diagrams in dark mode when Joplin is in dark mode (#9631) 2024-01-04 13:45:06 +00:00
lib
MdToHtml Desktop: Fixes #9829: Fix mermaid save button creates additional space above diagrams (#9830) 2024-02-02 23:57:57 +01:00
tests
Tools
vendor
.gitignore
assetsToHeaders.ts Chore: Refactor renderer package: Limit dependency on @joplin/lib and improve type safety (#9701) 2024-01-18 11:20:10 +00:00
defaultNoteStyle.js Desktop: Better handling of bold text to simplify customisation (#5732) 2021-12-28 09:57:34 +00:00
defaultResourceModel.ts Chore: Refactor renderer package: Limit dependency on @joplin/lib and improve type safety (#9701) 2024-01-18 11:20:10 +00:00
headerAnchor.ts Server: Added Help page for Joplin Cloud 2021-08-31 13:46:46 +01:00
highlight.ts Chore: Optimize highlight.js package size 2022-05-26 16:46:56 +01:00
HtmlToHtml.test.ts Desktop: Fixes #8485: Note imported from Web Clipper is broken after being saved from the Rich Text editor 2023-07-26 17:37:24 +01:00
HtmlToHtml.ts Chore: Refactor renderer package: Limit dependency on @joplin/lib and improve type safety (#9701) 2024-01-18 11:20:10 +00:00
htmlUtils.test.ts Desktop: Fixed copying and pasting an image from Chrome in RTE 2023-11-17 18:11:17 +00:00
htmlUtils.ts Desktop: Fixes #9304: Fix HTML resource links lost when editing notes in the rich text editor (#9435) 2023-12-06 19:17:16 +00:00
index.ts Chore: Refactor renderer package: Limit dependency on @joplin/lib and improve type safety (#9701) 2024-01-18 11:20:10 +00:00
InMemoryCache.ts Tools: Apply eslint rule @typescript-eslint/no-inferrable-types with ignoreArguments=false 2023-06-30 09:11:26 +01:00
jest.config.js Chore: Renderer: Refactor and test long-press and click handlers (#7774) 2023-02-17 13:13:28 +00:00
MarkupToHtml.ts Chore: Refactor renderer package: Limit dependency on @joplin/lib and improve type safety (#9701) 2024-01-18 11:20:10 +00:00
MdToHtml.ts Chore: Refactor renderer package: Limit dependency on @joplin/lib and improve type safety (#9701) 2024-01-18 11:20:10 +00:00
noteStyle.ts Desktop: Fixes #9511: HTML notes are not readable in dark mode 2023-12-29 16:08:09 +00:00
package.json Update dependency @types/node to v18.19.8 (#9866) 2024-02-07 02:35:16 +00:00
pathUtils.js Server v2.14.2 2024-01-18 17:23:51 +00:00
publish.sh Chore: Rename instances of yarn run to just yarn 2024-01-26 20:19:28 +00:00
README.md
stringUtils.js Tools: Implement "prefer-object-spread" eslint rule 2023-06-01 12:02:36 +01:00
tsconfig.json Chore: Tests: Fix vscode doesn't recognize Jest types in some test files (#9337) 2023-11-17 16:04:36 +00:00
types.ts Chore: Refactor renderer package: Limit dependency on @joplin/lib and improve type safety (#9701) 2024-01-18 11:20:10 +00:00
urlUtils.js
utils.ts Chore: Refactor renderer package: Limit dependency on @joplin/lib and improve type safety (#9701) 2024-01-18 11:20:10 +00: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

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