Skip to content

Commit 2060155

Browse files
NathanYiNathanYi
authored andcommitted
chore(mdx support): updated prettierIgnore with mdx style and added test to check
1 parent 55931ef commit 2060155

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/__tests__/md-inject.test.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ The output of some arbitrary command
333333

334334
const outFile = `
335335
{/* CODEBLOCK_START {"type":"command","value":"some arbitrary command"} */}
336-
<!-- prettier-ignore -->
336+
{/* prettier-ignore */}
337337
~~~~~~~~~~bash
338338
$ some arbitrary command
339339
@@ -344,6 +344,35 @@ The output of some arbitrary command
344344
expect(fs.writeFile).toHaveBeenCalledWith('foo.mdx', outFile)
345345
})
346346

347+
it('fails to write to the markdown document (command) with mixed syntax', async () => {
348+
const inFile = `
349+
{/* CODEBLOCK_START {"type":"command","value":"some arbitrary command"} */}
350+
351+
{/* CODEBLOCK_END */}`
352+
353+
const inFileName = `<!-- prettier-ignore -->
354+
~~~~~~~~~~bash
355+
$ some arbitrary command
356+
357+
The output of some arbitrary command
358+
~~~~~~~~~~`
359+
360+
glob.mockResolvedValue([inFileName])
361+
362+
fs.readFile.mockImplementation(async (fileName) => {
363+
if (fileName === inFileName) {
364+
return inFile
365+
}
366+
throw new Error('Unexpected file name passed')
367+
})
368+
369+
await injectMarkdown()
370+
371+
expect(fs.readFile).toHaveBeenCalledWith(inFileName, { encoding: 'utf-8' })
372+
373+
expect(fs.writeFile).not.toHaveBeenCalled()
374+
})
375+
347376
it('does not write to the markdown document (command) because of bad syntax', async () => {
348377
const inFile = `
349378
<!-- CODEBLOCK_START {"type":"command","value":"some arbitrary command"} */}

src/md-inject.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,10 @@ const main = async (
207207
// https://github.github.com/gfm/#example-94
208208
const codeblockFence = '~~~~~~~~~~'
209209

210-
const prettierIgnore = '<!-- prettier-ignore -->'
210+
const checkFileName = fileName
211+
const prettierIgnore = checkFileName.includes('mdx')
212+
? '{/* prettier-ignore */}'
213+
: '<!-- prettier-ignore -->'
211214

212215
if (trim) {
213216
out = out.trim()

0 commit comments

Comments
 (0)