test(plugin-md-power): add audioReader unit tests (#401)

This commit is contained in:
pengzhanbo 2024-12-29 00:38:30 +08:00 committed by GitHub
parent 2e28989a09
commit b5c7b0536d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 74 additions and 2 deletions

View File

@ -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>
"
`;

View File

@ -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()
})
})

View File

@ -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]]
@ -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