Skip to content

Commit

Permalink
test(plugin-md-power): add audioReader unit tests (#401)
Browse files Browse the repository at this point in the history
  • Loading branch information
pengzhanbo authored Dec 28, 2024
1 parent 2e28989 commit b5c7b05
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`artPlayerPlugin > should not work 1`] = `
"<p>@[audioReader @<a href="">audioReader</a></p>
<p>@[audioReader]xxx</p>
<p>@[ audioReader](123456</p>
<p>@[audioReader]((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((()</p>
<p>@<a href="/xxx.mp3"> audioReader</a> xxx</p>
<p>@<a href="xxx.mp3">audioReader</a> xxx</p>
<p>@[audioReader](javascript:alert(1)) xxx</p>
"
`;

exports[`artPlayerPlugin > should work 1`] = `
"<p><AudioReader src="/xxx.mp3"></AudioReader> <AudioReader src="/xxx.mp3"></AudioReader></p>
<p><AudioReader src="/xxx.mp3">title</AudioReader></p>
<p><AudioReader src="/xxx.mp3" :start-time="0" :end-time="99" :volume="0.55"></AudioReader></p>
<p>xxx <AudioReader src="/xxx.mp3" type="audio/mp3"></AudioReader> xxx</p>
"
`;
48 changes: 48 additions & 0 deletions plugins/plugin-md-power/__test__/audioReader.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import MarkdownIt from 'markdown-it'
import { describe, expect, it } from 'vitest'
import { audioReaderPlugin } from '../src/node/embed/audio/reader.js'

function createMarkdown() {
return MarkdownIt().use((md) => {
md.block.ruler.before('code', 'import_code', () => false)
md.renderer.rules.import_code = () => ''
}).use(audioReaderPlugin)
}

describe('artPlayerPlugin', () => {
it('should work', () => {
const md = createMarkdown()
const code = `\
@[audioReader](/xxx.mp3) @[audioReader](/xxx.mp3)
@[audioReader autoplay title="title"](/xxx.mp3)
@[audioReader autoplay start-time="0" end-time="99" volume="0.55"](/xxx.mp3)
xxx @[audioReader type="audio/mp3"](/xxx.mp3) xxx
`

expect(md.render(code)).toMatchSnapshot()
})

it('should not work', () => {
const md = createMarkdown()
const code = `\
@[audioReader @[audioReader]()
@[audioReader]xxx
@[ audioReader](123456
@[audioReader]((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((()
@[ audioReader](/xxx.mp3) xxx
@[audioReader]( xxx.mp3) xxx
@[audioReader](javascript:alert(1)) xxx
`

expect(md.render(code)).toMatchSnapshot()
})
})
8 changes: 6 additions & 2 deletions plugins/plugin-md-power/src/node/embed/audio/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,18 @@ const audioReader: RuleInline = (state, silent) => {
return false
}
}
else {
return false
}

/* istanbul ignore else -- @preserve */
if (!silent) {
state.pos = labelStart
state.posMax = labelEnd
const info = state.src.slice(labelStart, labelEnd).trim()
const { attrs } = resolveAttrs(info)

const tokenOpen = state.push('audio_reader_open', 'audioReader', 1)
const tokenOpen = state.push('audio_reader_open', 'AudioReader', 1)
tokenOpen.info = info
tokenOpen.attrs = [['src', href]]

Expand All @@ -69,7 +73,7 @@ const audioReader: RuleInline = (state, silent) => {
if (attrs.title)
state.push('text', '', 0).content = attrs.title

state.push('audio_reader_close', 'audioReader', -1)
state.push('audio_reader_close', 'AudioReader', -1)
}

state.pos = pos + 1
Expand Down

0 comments on commit b5c7b05

Please sign in to comment.