Created
September 17, 2024 23:10
-
-
Save skeptrunedev/88c4f3227e45e6fe3cf2dc84b01cd12b to your computer and use it in GitHub Desktop.
Convert Markdown to HTML using Github API
This file contains hidden or 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
| const fs = require('fs'); | |
| const childProcess = require('child_process'); | |
| const GITHUB_ACCESS_TOKEN = process.env.GITHUB_ACCESS_TOKEN; | |
| const inputDirectory = './docs'; | |
| const outputDirectory = './htmldocs'; | |
| fs.readdirSync(inputDirectory).filter(file => file.endsWith('.md')).forEach(file => { | |
| const filePath = `${inputDirectory}/${file}`; | |
| const markdownContent = fs.readFileSync(filePath, 'utf8'); | |
| const htmlContent = convertMarkdownToHtml(markdownContent); | |
| const outputFilePath = `${outputDirectory}/${file.replace('.md', '.html')}`; | |
| fs.writeFileSync(outputFilePath, htmlContent); | |
| }); | |
| function convertMarkdownToHtml(markdownContent) { | |
| const curlCommand = `curl https://api.github.com/markdown/raw -s -XPOST -H Content-Type:text/plain -H Authorization:'Bearer ${GITHUB_ACCESS_TOKEN}' -d @-`; | |
| const response = childProcess.execSync(curlCommand, { input: markdownContent }); | |
| return response.toString('utf8'); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment