-
Notifications
You must be signed in to change notification settings - Fork 39
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
command line script for node #84
base: main
Are you sure you want to change the base?
Changes from all commits
fb90c00
6778c06
af6e63c
c173b6a
e589863
e295a2f
ee8e4b4
f08683d
99fa8f4
84d5c45
3708b0e
16edcfd
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/usr/bin/env node | ||
|
||
import {convertDocsHtmlToMarkdown} from './lib/convert.js'; | ||
import { buffer as readBuffer } from 'node:stream/consumers'; | ||
|
||
// in order to debug this tool over the command line, you can read a file with broken input locally | ||
// ex: fs.readFileSync('./local-file.html', 'utf8'); | ||
|
||
const rawInput = await readBuffer(process.stdin); | ||
const inputHTML = rawInput.toString('utf-8'); | ||
|
||
if(!inputHTML) { | ||
console.error('no HTML provided over stdin'); | ||
process.exit(1); | ||
} | ||
|
||
try { | ||
convertDocsHtmlToMarkdown(inputHTML).then(markdown => { | ||
process.stdout.write(markdown); | ||
}); | ||
} catch (error) { | ||
console.error(`Error converting HTML to markdown: ${error.message}`); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
import fixGoogleHtml from './fix-google-html.js'; | ||
// rehype-dom-parse is a lightweight version of rehype-parse that leverages | ||
// browser APIs -- reduces bundle size by ~200 kB! | ||
import parse from 'rehype-dom-parse'; | ||
import {default as rehypeDom} from 'rehype-dom-parse'; | ||
import {default as rehypeNode} from 'rehype-parse'; | ||
import { all } from 'rehype-remark'; | ||
import rehype2remarkWithSpaces from './rehype-to-remark-with-spaces.js'; | ||
import remarkGfm from 'remark-gfm'; | ||
import stringify from 'remark-stringify'; | ||
import { unified } from 'unified'; | ||
import { default as logTree } from './log-tree.js'; | ||
|
||
/** @typedef {import("mdast-util-to-markdown").State} as MdastState */ | ||
/** @typedef {import("unist").Node} UnistNode */ | ||
|
@@ -35,10 +37,14 @@ function doubleBlankLinesBeforeHeadings (previous, next, _parent, _state) { | |
return undefined; | ||
} | ||
|
||
const isNode = typeof process !== 'undefined' && process.versions?.node; | ||
const rehypeParse = isNode ? rehypeNode : rehypeDom; | ||
const rehypeParseOptions = isNode ? {fragment: true, verbose: true, emitParseErrors: true} : {} | ||
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 there an important reason to have different parser options on Node vs. browsers? I know the default for Why set Why set 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. I can't remember exactly, but I believe the existing parser just didn't work at all/well when I was testing. |
||
|
||
const processor = unified() | ||
.use(parse) | ||
.use(rehypeParse, rehypeParseOptions) | ||
.use(fixGoogleHtml) | ||
// .use(require('./lib/log-tree').default) | ||
// .use(logTree) | ||
.use(rehype2remarkWithSpaces, { | ||
handlers: { | ||
// Preserve sup/sub markup; most Markdowns have no markup for 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.
I think we’d be better off here to require just
rehype-parse
, and substituterehype-dom-parse
during Webpack compilation. (Thenrehype-dom-parse
can also just be a dev-time dependency.)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.
I'm not familiar with webpack—what's the best way to do this? Can you submit a change for this PR for it?