Skip to content

Commit

Permalink
fix: don't parse import/export statements in text
Browse files Browse the repository at this point in the history
  • Loading branch information
brc-dd committed Dec 2, 2024
1 parent 473cfc0 commit c2edb6c
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
dist
vendor
6 changes: 4 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
},
"tasks": {
"dev": "deno run -A --watch=src ./scripts/build.ts",
"build": "deno run -A ./scripts/build.ts",
"build": "deno task patch && deno run -A ./scripts/build.ts",
"format": "deno fmt",
"format:fail": "deno fmt --check",
"lint": "deno lint --fix",
"lint:fail": "deno lint",
"test": "deno test -A",
"test:watch": "deno test -A --watch",
"type": "deno check **/*.ts",
"check": "deno task format:fail && deno task lint:fail && deno task type && deno task test",
"check": "deno task patch && deno task format:fail && deno task lint:fail && deno task type && deno task test",
"patch": "deno run -A ./scripts/patch.ts",
"release": "deno run -A --reload https://raw.githubusercontent.com/globalbrain/hado/main/scripts/release.ts",
"update": "deno run -A --reload https://raw.githubusercontent.com/globalbrain/hado/main/scripts/update.ts"
},
Expand All @@ -28,6 +29,7 @@
"singleQuote": true
},
"imports": {
"@ast-grep/napi": "npm:@ast-grep/napi@^0.30.1",
"@luca/esbuild-deno-loader": "jsr:@luca/esbuild-deno-loader@^0.11.0",
"@std/assert": "jsr:@std/assert@^1.0.8",
"esbuild": "npm:esbuild@^0.24.0",
Expand Down
43 changes: 43 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions scripts/patch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Lang, parse } from '@ast-grep/napi'
import denoJson from '../deno.json' with { type: 'json' }

const version = denoJson.imports.prettier.split('@')[1]

const res = await fetch(`https://esm.sh/v135/prettier@${version}/es2022/plugins/markdown.js`)
const code = (await res.text()).replace(/\/\/# sourceMappingURL=.*\.js\.map/g, '')

const root = parse(Lang.JavaScript, code).root()

const node1 = root.find('$A&&{blocks:[$B]}')!
const n1$A = node1.getMatch('A')!.text()

const node2 = root.find(`$A.use(${n1$A}?$B:$C).use($D).use`)!
const n2$A = node2.getMatch('A')!.text()
const n2$D = node2.getMatch('D')!.text()

const edit1 = node2.replace(`${n2$A}.use(${n2$D}).use`)
const transformed = root.commitEdits([edit1]).toString()

await Deno.writeTextFile('./vendor/prettier-plugin-markdown.js', transformed)
await Deno.writeTextFile(
'./vendor/prettier-plugin-markdown.d.ts',
`
import type { Parser, Plugin, Printer } from 'prettier'
declare const md: Plugin & {
parsers: Record<'markdown' | 'mdx' | 'remark', Parser>
printers: { mdast: Printer & Required<Pick<Printer, 'embed'>> }
}
export default md
`.trimStart(),
)
11 changes: 3 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import type { Literal } from 'npm:@types/unist@2'
import type { AstPath, Parser, Plugin, Printer } from 'prettier'
import type { AstPath, Plugin } from 'prettier'
import { type builders, printer, utils } from 'prettier/doc'
import _md from 'prettier/plugins/markdown'

const md = _md as unknown as Plugin & {
parsers: Record<'markdown' | 'mdx' | 'remark', Parser>
printers: { mdast: Printer & Required<Pick<Printer, 'embed'>> }
}
// @deno-types="../vendor/prettier-plugin-markdown.d.ts"
import md from '../vendor/prettier-plugin-markdown.js'

const wrappedRE = /^\s*<(script|style)\b/

const plugin: Plugin = {
...md,
parsers: {
...md.parsers,
// TODO: only keep blocks rule, disable others
// (currently messing up text containing import/export)
markdown: md.parsers.mdx,
},
printers: {
Expand Down

0 comments on commit c2edb6c

Please sign in to comment.