-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e18919e
commit 55b81f2
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#!/usr/bin/env node | ||
|
||
import fs from 'fs'; | ||
import {convertDocsHtmlToMarkdown} from './lib/convert.js'; | ||
|
||
// to avoid top-level async/await. readFileSync(0) caused strange errors | ||
// https://stackoverflow.com/questions/67037353/any-way-to-read-from-stdin-synchronously-without-extra-modules-libraries-node-j | ||
function stdinReadSync() { | ||
if (process.stdin.isTTY) { | ||
return ''; | ||
} | ||
|
||
const b = Buffer.alloc(1024) | ||
let data = '' | ||
|
||
while (true) { | ||
let n = 0 | ||
|
||
// Read while EAGAIN | ||
while (true) { | ||
try { | ||
n = fs.readSync(process.stdin.fd, b, 0, b.length) | ||
break | ||
} catch (e) { | ||
if (e.code === 'EAGAIN') { | ||
// Sleep 100ms | ||
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 100) | ||
continue | ||
} | ||
throw e | ||
} | ||
} | ||
|
||
if (!n) break | ||
data += b.toString('utf8', 0, n) | ||
} | ||
|
||
return data | ||
} | ||
|
||
// 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 inputHTML = stdinReadSync() | ||
|
||
if(!inputHTML) { | ||
console.error('No input HTML provided. Please pipe HTML to this command.'); | ||
process.exit(1); | ||
} | ||
|
||
convertDocsHtmlToMarkdown(inputHTML).then(markdown => { | ||
process.stdout.write(markdown); | ||
}); |