Skip to content

Commit

Permalink
fix: don't format mdit attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
brc-dd committed Dec 2, 2024
1 parent bdc7913 commit 8b5e9a0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ await esbuild.build({
format: 'esm',
external: ['prettier'],
minify: !!Deno.env.get('CI'),
target: 'node18',
})

await Deno.writeTextFile(
Expand Down
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ const plugin: Plugin = {
embed: (path: AstPath<Literal<string>>, options) => {
const { node } = path

if (node.type === 'word' && (node.value.at(0) === '{' || node.value.at(-1) === '}')) {
// attr (FIXME: do better parsing)
return node.value
}

if ((node.type !== 'jsx' && node.type !== 'liquidNode') || node.value.includes('\uFFFF')) {
return md.printers.mdast.embed(path, options)
}
Expand All @@ -47,6 +52,7 @@ const plugin: Plugin = {

return node.value // skip formatting something went wrong

// markdown in vue
// return formatted.split('\uFFFF')[0]?.replace(/^\s{2}/gm, '')
}
},
Expand Down
30 changes: 30 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,21 @@ comment
</template>
</Example>
</div>
## 10
- export \`foo()\` (some long text here, this should not be treated as es export statement)
## 11
\`\`\`md
<a href="/pure.html" target="_self" >Link to pure.html</a>
\`\`\`
## 12
attrs {target="_self" #id}
attrs {target="_self"}
`

const expected = `
Expand Down Expand Up @@ -160,6 +175,21 @@ comment
</template>
</Example>
</div>
## 10
- export \`foo()\` (some long text here, this should not be treated as es export statement)
## 11
\`\`\`md
<a href="/pure.html" target="_self">Link to pure.html</a>
\`\`\`
## 12
attrs {target="_self" #id}
attrs {target="_self"}
`.trimStart()

const result = await prettier.format(code, {
Expand Down

0 comments on commit 8b5e9a0

Please sign in to comment.