diff --git a/readme.md b/readme.md index e3e38ff..10b50a1 100644 --- a/readme.md +++ b/readme.md @@ -27,14 +27,14 @@ npm run tsc ## Programatic Use ```ts -import { MarkdownOptions, MarkdownExtension, render_markdown_to_html } from './build'; +import { MarkdownOptions, render_markdown_to_html } from './build'; async function main() { - const opts: MarkdownOptions = { - // ... + const options: MarkdownOptions = { + base_url: 'https://example.com', }; - const html = await render_markdown_to_html('# This is some markdown', opts); + const html = await render_markdown_to_html('# This is some markdown', options); } ``` @@ -43,6 +43,88 @@ async function main() { ## Command Line Use ```bash -echo '# This is some markdown' | ./bin/markdown2html [...options] > output.html +echo '# This is some markdown' | ./bin/markdown2html --base-url 'https://example.com' > output.html +``` + + + +## Options + +- `options.base_url` / `--base-url` - Sets the Marked `baseUrl` option +- `options.breaks` / `--breaks` - Sets the Marked `breaks` option +- `options.inline` / `--inline` - Sets the Marked `inline` option +- `options.katex_macros` / `--katex-macro` - Defines any global Katex macros to be used. +- `options.extensions` - Any additional Marked extension factories to use + +#### Katex Macro Examples + +```ts +const options: MarkdownOptions = { + katex_macros: { + '\\foobar': '\\text{foo} + \\text{bar}', + '\\bazqux': '\\text{baz} + \\text{qux}', + } +}; +``` + +```bash +./bin/markdown2html \ + --katex-macro '\foobar' '\text{foo}' '\text{bar}' \ + --katex-macro '\bazqux' '\text{baz}' '\text{qux}' +``` + + + + +## Extensions + +```ts +// extension.ts +import type { marked } from 'marked'; +import type { MarkdownOptions } from './build'; + +export interface MyCustomToken extends marked.Tokens.Generic { + // ... +} + +export function my_extension(renderer: marked.Renderer, options: MarkdownOptions) : marked.TokenizerExtension & marked.RendererExtension { + return { + name: 'my_extension', + level: 'inline', + start: (src) => src.match(/...../)?.index, + tokenizer(src, tokens) : MyCustomToken { + const rule = /...../; + const match = rule.exec(src); + + if (match) { + return { + // ... + }; + } + }, + renderer(token: MyCustomToken) { + return ''; + } + }; +}; + + + +// ---------- + +// main.ts +import { my_extension } from './extension'; +import { MarkdownOptions, render_markdown_to_html } from './build'; + +async function main() { + const options: MarkdownOptions = { + // ... + extensions: [ + my_extension + ] + }; + + const html = await render_markdown_to_html('# This is some markdown', options); +} ``` diff --git a/src/icon.ts b/src/icon.ts index ba65485..766ed3b 100644 --- a/src/icon.ts +++ b/src/icon.ts @@ -1,5 +1,5 @@ -import { marked } from 'marked'; +import type { marked } from 'marked'; import { icons } from './icons'; import { MarkdownOptions } from './render';