Skip to content

Commit

Permalink
NEW: Phonetic Transcriptions (#44)
Browse files Browse the repository at this point in the history
closes #44
  • Loading branch information
dwhieb committed Oct 4, 2024
1 parent 208374b commit 052cc28
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -67,6 +68,7 @@ export default class Parser {
.use(mathjax, createMathjaxInstance())
.use(ordinals)
.use(orthographic)
.use(phonetic)
.use(subscript)
.use(superscript)
.use(tableCaptions)
Expand Down
6 changes: 6 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ I love you
expect(html).to.equal(`<p>British English spells many words with a <span class="ortho">⟨u⟩</span>.</p>\n`)
})

it(`phonetic transcriptions`, function() {
const md = `The word [[ˈhɛ.loʊ]] is a greeting.`
const html = parser.parse(md)
expect(html).to.equal(`<p>The word <span class="fon">[ˈhɛ.loʊ]</span> is a greeting.</p>\n`)
})

it(`“smart quotes”`, function() {
const md = `"'Hello world!', he said."`
const html = parser.parse(md)
Expand Down
17 changes: 17 additions & 0 deletions plugins/phonetic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import iterator from 'markdown-it-for-inline'

const phoneticRegExp = /\[\[(?<txn>.+?)\]\]/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, `<span class="fon">[$<txn>]</span>`)
token.type = `html_inline`
}

})
}

0 comments on commit 052cc28

Please sign in to comment.