cb8dca747b
Refactor note editor using React Hooks and TypeScript and moved editor-specific code to separate files. Moved business logic into more maintainable custom hooks. Squashed commit of the following: commit f243d9bf89bdcfa1849ee26df5c0dd3e33405010 Author: Laurent Cozic <laurent@cozic.net> Date: Sat May 2 16:04:14 2020 +0100 Fixed saving issue commit 055f68d2e8b6cf6f130336c38ac2ab480887583d Author: Laurent Cozic <laurent@cozic.net> Date: Sat May 2 15:43:38 2020 +0100 Fixed HTML notes commit 99a3cf71f58d2fedcdf3001bf4110b6e8e3993da Merge: 9be85c45f2 |
||
---|---|---|
.. | ||
assets | ||
MdToHtml | ||
tests | ||
Tools | ||
vendor | ||
.gitignore | ||
assetsToHeaders.js | ||
defaultNoteStyle.js | ||
HtmlToHtml.js | ||
htmlUtils.js | ||
index.js | ||
MarkupToHtml.js | ||
MdToHtml.js | ||
noteStyle.js | ||
package-lock.json | ||
package.json | ||
pathUtils.js | ||
publish.sh | ||
README.md | ||
stringUtils.js | ||
urlUtils.js | ||
utils.js |
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 codepluginAssets
: 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',
},
];