Skip to content

Instantly share code, notes, and snippets.

@skeptrunedev
Created September 17, 2024 23:10
Show Gist options
  • Select an option

  • Save skeptrunedev/88c4f3227e45e6fe3cf2dc84b01cd12b to your computer and use it in GitHub Desktop.

Select an option

Save skeptrunedev/88c4f3227e45e6fe3cf2dc84b01cd12b to your computer and use it in GitHub Desktop.
Convert Markdown to HTML using Github API
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