Skip to content

Commit

Permalink
NEW: Pass custom plugin options (#51)
Browse files Browse the repository at this point in the history
closes #51
  • Loading branch information
dwhieb committed Oct 8, 2024
1 parent 72cb0cd commit 86b9e6a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,13 @@ Parse a markdown string using the current options and return HTML.

### Options

| Option | Type | Default | Description |
| --------------- | --------- | ---------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `dlx2html` | Object | [See documentation here.][dlx2html] | Options to pass to `dlx2html` (the library that converts interlinear examples to HTML). If options are provided in a YAML header within fenced code blocks, those options override these ones. |
| `markdown` | Object | <pre><code>{<br> html: true,<br> typographer: true<br>}</code></pre> | Options to pass to `markdown-it`. `typographer` and `html` are enabled by default. |
| `translations` | `span\|q` | `span` | Whether to use `<span class=tln>` or a `<q>` element for translations. `<span>`s will wrap the inner text in single quotes. |
| `scription2dlx` | Object | [See documentation here.][scription2html] | Options to pass to `scription2html` (the library that parses interlinear examples). If options are provided in a YAML header within fenced code blocks, those options override these ones. |
| Option | Type | Default | Description |
| --------------- | --------- | ---------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `dlx2html` | Object | [See documentation here.][dlx2html] | Options to pass to `dlx2html` (the library that converts interlinear examples to HTML). If options are provided in a YAML header within fenced code blocks, those options override these ones. |
| `markdown` | Object | <pre><code>{<br> html: true,<br> typographer: true<br>}</code></pre> | Options to pass to `markdown-it`. `typographer` and `html` are enabled by default. |
| `plugins` | Object | `{}` | Options to pass to any of the `markdown-it` plugins. Each key should be the name of the plugin, and the value is the options to pass to it. Example: `{ '@mdit/plugin-alert': { /* options */ } }` You can see a complete list of the `markdown-it` plugins that are used in this library in the source code [here][source]. |
| `translations` | `span\|q` | `span` | Whether to use `<span class=tln>` or a `<q>` element for translations. `<span>`s will wrap the inner text in single quotes. |
| `scription2dlx` | Object | [See documentation here.][scription2html] | Options to pass to `scription2html` (the library that parses interlinear examples). If options are provided in a YAML header within fenced code blocks, those options override these ones. |

<!-- LINKS -->
[alert]: https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#alerts
Expand All @@ -137,6 +138,7 @@ Parse a markdown string using the current options and return HTML.
[Scription]: https://scription.digitallinguistics.io/
[scription2html]: https://github.com/digitallinguistics/scription2html
[spec]: https://github.com/digitallinguistics/ling-markdown-spec
[source]: https://github.com/digitallinguistics/ling-md/blob/main/index.js
[summary-details]: https://www.npmjs.com/package/markdown-it-collapsible
[table-captions]: https://github.com/martinring/markdown-it-table-captions
[tables]: https://www.markdownguide.org/extended-syntax/#tables
Expand Down
13 changes: 7 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default class Parser {
constructor({
dlx2html = {},
markdown = defaultMarkdownOptions,
plugins = {},
scription2dlx = {},
translations = `span`,
} = {}) {
Expand All @@ -53,29 +54,29 @@ export default class Parser {
this.engine = createMarkdownParser(markdownOptions)

this.engine
.use(alert)
.use(attributes) // Must come before headerAnchors
.use(alert, plugins[`@mdit/plugin-alert`])
.use(attributes, plugins[`markdown-it-attrs`]) // Must come before headerAnchors
.use(boldItalic)
.use(bracketedSpans)
.use(defLists)
.use(footnotes)
.use(fractions)
.use(glosses)
.use(headerAnchors)
.use(headerAnchors, plugins[`markdown-it-anchor`])
.use(inlineTranslations, { tag: translations })
.use(insertedText)
.use(interlinears, { dlx2html, scription2dlx })
.use(markedText)
.use(mathjax, createMathjaxInstance())
.use(mathjax, createMathjaxInstance(plugins[`@mdit/plugin-mathjax`]))
.use(ordinals)
.use(orthographic)
.use(phonemic)
.use(phonetic)
.use(subscript)
.use(superscript)
.use(tableCaptions)
.use(tasklist)
.use(toc)
.use(tasklist, plugins[`@mdit/plugin-tasklist`])
.use(toc, plugins[`markdown-it-table-of-contents`])

}

Expand Down
8 changes: 8 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,12 @@ I love you

})

it(`option: plugins`, function() {
const options = { plugins: { 'markdown-it-anchor': { permalink: false } } }
const md = `# Header`
const parser = new Parser(options)
const html = parser.parse(md)
expect(html).to.equal(`<h1 id="header" tabindex="-1">Header</h1>\n`)
})

})

0 comments on commit 86b9e6a

Please sign in to comment.