index.mjs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/env node
  2. import {marked} from 'marked'
  3. import hljs from 'highlight.js'
  4. import fs from 'node:fs'
  5. import path from 'node:path'
  6. import run from './html2pdf.mjs'
  7. // import 'highlight.js/styles/github.css';
  8. marked.setOptions({
  9. renderer: new marked.Renderer(),
  10. highlight: function(code, lang) {
  11. const language = hljs.getLanguage(lang) ? lang : 'plaintext';
  12. return hljs.highlight(code, { language }).value;
  13. },
  14. langPrefix: 'hljs language-', // highlight.js css expects a top-level 'hljs' class.
  15. pedantic: false,
  16. gfm: true,
  17. breaks: false,
  18. sanitize: false,
  19. smartLists: true,
  20. smartypants: false,
  21. xhtml: false
  22. });
  23. const filePath = `${process.argv[3]}`
  24. const markdownString = fs.readFileSync(filePath, 'utf8')
  25. const fileName = `${filePath}`.replace('.md','.html')
  26. console.log(fileName)
  27. let html = marked.parse(markdownString)
  28. html = `<link rel="stylesheet"
  29. href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/styles/github.min.css">
  30. <style>
  31. *{
  32. font-family: "PingFang TC";
  33. }
  34. h2 {
  35. margin-bottom: 5px;
  36. }
  37. h3 {
  38. margin-bottom: 0;
  39. }
  40. p{
  41. margin: 5px 0;
  42. }
  43. ol{
  44. margin: 5px 0;
  45. }
  46. </style>
  47. ${html}`
  48. try {
  49. fs.unlinkSync(fileName);
  50. } catch ( e ) {
  51. console.log(e)
  52. }
  53. fs.writeFile(fileName, html, { flag: 'a+' }, (err) => {
  54. if (err) throw err;
  55. run(fileName)
  56. })
  57. // console.log(marked.parse(markdownString));
  58. // import github from 'highlight.js/styles/github.css'
  59. // const github = fs.readFileSync('highlight.js/styles/github.css', 'utf8')
  60. // console.log(github)