diff --git a/index.js b/index.js index 629e5d5..8a94692 100644 --- a/index.js +++ b/index.js @@ -18,6 +18,7 @@ import interlinears from './plugins/interlinears.js' import markedText from 'markdown-it-mark' import ordinals from './plugins/ordinals.js' import orthographic from './plugins/orthographic.js' +import phonetic from './plugins/phonetic.js' import subscript from 'markdown-it-sub' import superscript from 'markdown-it-sup' import tableCaptions from 'markdown-it-table-captions' @@ -67,6 +68,7 @@ export default class Parser { .use(mathjax, createMathjaxInstance()) .use(ordinals) .use(orthographic) + .use(phonetic) .use(subscript) .use(superscript) .use(tableCaptions) diff --git a/index.test.js b/index.test.js index 771774b..633dfd1 100644 --- a/index.test.js +++ b/index.test.js @@ -182,6 +182,12 @@ I love you expect(html).to.equal(`

British English spells many words with a ⟨u⟩.

\n`) }) + it(`phonetic transcriptions`, function() { + const md = `The word [[ˈhɛ.loʊ]] is a greeting.` + const html = parser.parse(md) + expect(html).to.equal(`

The word [ˈhɛ.loʊ] is a greeting.

\n`) + }) + it(`“smart quotes”`, function() { const md = `"'Hello world!', he said."` const html = parser.parse(md) diff --git a/plugins/phonetic.js b/plugins/phonetic.js new file mode 100644 index 0000000..8977201 --- /dev/null +++ b/plugins/phonetic.js @@ -0,0 +1,17 @@ +import iterator from 'markdown-it-for-inline' + +const phoneticRegExp = /\[\[(?.+?)\]\]/gv + +export default function phonetic(md) { + md.use(iterator, `phonetic`, `text`, (tokens, i) => { + + const token = tokens[i] + const text = token.content + + if (text.match(phoneticRegExp)) { + token.content = text.replaceAll(phoneticRegExp, `[$]`) + token.type = `html_inline` + } + + }) +}