-
-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add browser support #9
Changes from 4 commits
710d19e
a252a6a
9365b0a
ef608d2
e1a698c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"100": true, | ||
"include": ["index.ts"], | ||
"reporter": ["html", "lcov", "text"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,7 @@ | ||
extends: | ||
- remcohaszing | ||
rules: | ||
no-param-reassign: off | ||
'@typescript-eslint/consistent-type-imports': | ||
- error | ||
- prefer: type-imports |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
coverage/ | ||
dist/ | ||
node_modules/ | ||
test-results/ | ||
*.d.ts | ||
*.js | ||
*.log | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
__fixtures__/*/output.md | ||
__snapshots__/ | ||
**/*-snapshots/* | ||
coverage/ | ||
dist/ |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,39 @@ | ||||||
import { fromDom } from 'hast-util-from-dom'; | ||||||
import { type Code, type Parent, type Root } from 'mdast'; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this equivalent to
Suggested change
? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, those are equivalent. Because there are no ESLint rules to force a consistent way to write type imports, I have decided to disallow type imports in my personal ESLint config. However, Playwright doesn’t support unannotated type imports, so I decided to enforce type imports in this project for consistency and use a |
||||||
import mermaid from 'mermaid'; | ||||||
import { type Plugin } from 'unified'; | ||||||
import { visit } from 'unist-util-visit'; | ||||||
|
||||||
// eslint-disable-next-line jsdoc/require-jsdoc | ||||||
function transformer(ast: Root): void { | ||||||
const instances: [string, number, Parent][] = []; | ||||||
|
||||||
visit(ast, { type: 'code', lang: 'mermaid' }, (node: Code, index, parent: Parent) => { | ||||||
instances.push([node.value, index, parent]); | ||||||
}); | ||||||
|
||||||
// Nothing to do. No need to start puppeteer in this case. | ||||||
if (!instances.length) { | ||||||
return; | ||||||
} | ||||||
|
||||||
const results = instances.map(([code], index) => | ||||||
// @ts-expect-error The mermaid types are wrong. | ||||||
mermaid.render(`remark-mermaid-${index}`, code), | ||||||
); | ||||||
|
||||||
const wrapper = document.createElement('div'); | ||||||
for (const [i, [, index, parent]] of instances.entries()) { | ||||||
const value = results[i]; | ||||||
wrapper.innerHTML = value; | ||||||
parent.children.splice(index, 1, { | ||||||
type: 'paragraph', | ||||||
children: [{ type: 'html', value }], | ||||||
data: { hChildren: [fromDom(wrapper.firstChild!)] }, | ||||||
}); | ||||||
} | ||||||
} | ||||||
|
||||||
const remarkMermaid: Plugin<[], Root> = () => transformer; | ||||||
|
||||||
export default remarkMermaid; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to cache the browser binaries installed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes total sense! I’ll look into it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
microsoft/playwright#7249 may offer some ideas