Skip to content

Commit

Permalink
test: add e2e test with vPre plugin in importCodePlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope committed May 21, 2024
1 parent 0d91448 commit 4b276a6
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`@vuepress/markdown > plugins > importCodePlugin > compatibility with otherPlugin > should preserve the things after code as fence info 1`] = `
"<pre><code class="language-js{1,3-4}">const msg = 'hello from js'
console.log(msg)
console.log('foo bar')
</code></pre>
"
`;

exports[`@vuepress/markdown > plugins > importCodePlugin > compatibility with otherPlugin > should preserve the things after code as fence info 2`] = `
"<pre><code class="language-md:no-line-numbers:no-v-pre"># msg
hello from md
## foo bar
</code></pre>
"
`;
66 changes: 65 additions & 1 deletion packages/markdown/tests/plugins/importCodePlugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
it,
vi,
} from 'vitest'
import { importCodePlugin } from '../../src/index.js'
import { importCodePlugin, vPrePlugin } from '../../src/index.js'
import type { MarkdownEnv } from '../../src/index.js'

const jsFixturePathRelative = '../__fixtures__/importCode.js'
Expand Down Expand Up @@ -282,4 +282,68 @@ foo
expect(mockConsoleError).toHaveBeenCalledTimes(1)
})
})

describe('compatibility with otherPlugin', () => {
it('should preserve the things after code as fence info', () => {
const source1 = `\
@[code js{1,3-4}](${jsFixturePathRelative})
`
const source2 = `\
@[code md:no-line-numbers:no-v-pre title="no-line-numbers.md"](${mdFixturePathRelative})
`

const md = MarkdownIt().use(importCodePlugin)
const env1: MarkdownEnv = {
filePath: __filename,
}

const rendered1 = md.render(source1, env1)

expect(rendered1).toEqual(
md.render(`\
\`\`\`js{1,3-4}
${jsFixtureContent}\
\`\`\`
`),
)
expect(rendered1).toMatchSnapshot()
expect(env1.importedFiles).toEqual([jsFixturePath])

const env2: MarkdownEnv = {
filePath: __filename,
}

const rendered2 = md.render(source2, env2)

expect(rendered2).toEqual(
md.render(`\
\`\`\`md:no-line-numbers:no-v-pre title="no-line-numbers.md"
${mdFixtureContent}\
\`\`\`
`),
)
expect(rendered2).toMatchSnapshot()
expect(env2.importedFiles).toEqual([mdFixturePath])
})

it('should work with syntax supported by vPrePlugin', () => {
const source1 = `\
@[code js{1,3-4}](${jsFixturePathRelative})
`
const source2 = `\
@[code md:no-line-numbers:no-v-pre title="no-line-numbers.md"](${mdFixturePathRelative})
`

const md = MarkdownIt().use(importCodePlugin).use(vPrePlugin)
const env: MarkdownEnv = {
filePath: __filename,
}

const rendered1 = md.render(source1, env)
const rendered2 = md.render(source2, env)

expect(rendered1).to.contain('v-pre')
expect(rendered2).to.not.contain(' v-pre')
})
})
})

0 comments on commit 4b276a6

Please sign in to comment.