Skip to content

Commit

Permalink
NEW: Allow users to pass options to DLx libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
dwhieb committed Sep 21, 2024
1 parent a155302 commit 5964f92
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ The library enables the following features by default:

### Linguistics

| Feature | Markdown | HTML |
| ------------------- | --------------------------------- | ------------------------------------------------ |
| grammatical glosses | `^^fut^^` | `<abbr class="gl">fut</abbr>` |
| inline examples | `The word *perro* is Spanish.` | `<p>The word <i>perro</i> is Spanish.</p>` |
| inline translations | `The word *perro* means ''dog''.` | `<p>The word <i>perro</i> means <q>dog</q>.</p>` |
| interlinear glosses | <pre><code>\```igl<br>ninakupenda<br>ni-na-ku-pend-a<br>1sg.SUBJ-PRES-2sg.OBJ-love-IND<br>I love you<br>```</code></pre> | [See documentation here.][dlx2html] |
| Feature | Markdown | HTML |
| ------------------- | ------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------ |
| grammatical glosses | `^^fut^^` | `<abbr class="gl">fut</abbr>` |
| inline examples | `The word *perro* is Spanish.` | `<p>The word <i>perro</i> is Spanish.</p>` |
| inline translations | `The word *perro* means ''dog''.` | `<p>The word <i>perro</i> means <q>dog</q>.</p>` |
| interlinear glosses | <pre><code>\```igl<br>ninakupenda<br>ni-na-ku-pend-a<br>1sg.SUBJ-PRES-2sg.OBJ-love-IND<br>I love you<br>```</code></pre> | [See documentation here.][dlx2html] |

### General

Expand Down Expand Up @@ -117,10 +117,12 @@ Parse a markdown string using the current options and return HTML.

### Options

| Option | Type | Default | Description |
| -------------- | --------- | ---------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------- |
| `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. |
| 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. |

<!-- 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 @@ -133,6 +135,7 @@ Parse a markdown string using the current options and return HTML.
[i]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/i
[markdown-it]: https://github.com/markdown-it/markdown-it#readme
[Scription]: https://scription.digitallinguistics.io/
[scription2html]: https://github.com/digitallinguistics/scription2html
[spec]: https://github.com/digitallinguistics/ling-markdown-spec
[summary-details]: https://www.npmjs.com/package/markdown-it-collapsible
[table-captions]: https://github.com/martinring/markdown-it-table-captions
Expand Down
8 changes: 5 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ export default class Parser {
* Create a new linguistics markdown parser instance.
*/
constructor({
markdown = defaultMarkdownOptions,
translations = `span`,
dlx2html = {},
markdown = defaultMarkdownOptions,
scription2dlx = {},
translations = `span`,
} = {}) {

const markdownOptions = Object.assign({}, defaultMarkdownOptions, markdown)
Expand All @@ -59,7 +61,7 @@ export default class Parser {
.use(headerAnchors)
.use(inlineTranslations, { tag: translations })
.use(insertedText)
.use(interlinears)
.use(interlinears, { dlx2html, scription2dlx })
.use(markedText)
.use(mathjax, createMathjaxInstance())
.use(ordinals)
Expand Down
22 changes: 16 additions & 6 deletions plugins/interlinears.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ import scription2dlx from '@digitallinguistics/scription2dlx'

const yamlHeaderRegExp = /^---\n(?<header>.*?)\n---\n/sv

export default function InterlinearsPlugin(md) {
const defaultOptions = {
dlx2html: {
glosses: true,
tag: `li`,
},
scription2dlx: {},
}

export default function InterlinearsPlugin(md, pluginOptions = {}) {

const originalRenderer = md.renderer.rules.fence

Expand All @@ -16,11 +24,13 @@ export default function InterlinearsPlugin(md) {

if (lang === `igl`) {

const header = token.content.match(yamlHeaderRegExp)?.groups?.header
const options = header ? jsYaml.load(header) : {}
const scription = token.content.replace(yamlHeaderRegExp, ``)
const data = scription2dlx(scription, options.scription2dlx)
const html = dlx2html(data, options.dlx2html)
const header = token.content.match(yamlHeaderRegExp)?.groups?.header
const yamlOptions = header ? jsYaml.load(header) : {}
const scription = token.content.replace(yamlHeaderRegExp, ``)
const scription2dlxOptions = Object.assign({}, defaultOptions.scription2dlx, pluginOptions.scription2dlx, yamlOptions.scription2dlx)
const data = scription2dlx(scription, scription2dlxOptions)
const dlx2htmlOptions = Object.assign({}, defaultOptions.dlx2html, pluginOptions.dlx2html, yamlOptions.dlx2html)
const html = dlx2html(data, dlx2htmlOptions)

return html

Expand Down

0 comments on commit 5964f92

Please sign in to comment.