remark.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { mdxAnnotations } from 'mdx-annotations'
  2. import remarkGfm from 'remark-gfm'
  3. import remarkMdx from 'remark-mdx'
  4. import remarkFrontmatter from 'remark-frontmatter'
  5. import remarkParseFrontmatter from 'remark-parse-frontmatter'
  6. import remarkMdxFrontmatter from 'remark-mdx-frontmatter'
  7. import remarkSmartypants from 'remark-smartypants'
  8. import remarkUnwrapImages from 'remark-unwrap-images'
  9. import remarkMdxCodeMeta from 'remark-mdx-code-meta';
  10. import remarkToc from 'remark-toc'
  11. import remarkGithub from 'remark-github'
  12. import remarkMath from 'remark-math'
  13. // import { toMarkdown } from 'mdast-util-to-markdown'
  14. // import { frontmatterToMarkdown } from 'mdast-util-frontmatter'
  15. // import { mdxjsEsmToMarkdown } from 'mdast-util-mdxjs-esm'
  16. // import { mdxJsxToMarkdown } from 'mdast-util-mdx-jsx'
  17. // import { gfmTableToMarkdown } from 'mdast-util-gfm-table'
  18. // import * as acorn from 'acorn'
  19. // import jsx from 'acorn-jsx'
  20. // const remarkLayout = () => {
  21. // const parser = acorn.Parser.extend(jsx())
  22. // let layout = 'BasicLayout'
  23. // return (tree, file) => {
  24. // if(file.history[0].match(/pages\/docs\//)) {
  25. // layout = 'DocsLayout'
  26. // } else if(file.history[0].match(/pages\/guides\//)) {
  27. // layout = 'GuideLayout'
  28. // } else if(file.history[0].match(/pages\/changelog\//)) {
  29. // layout = 'ChangelogLayout'
  30. // }
  31. // console.log(file.history[0], layout);
  32. // let exportPropsStr = `export async function getStaticProps() { return { props: { meta }}}`
  33. // tree.children.push({
  34. // type: 'mdxjsEsm',
  35. // value: exportPropsStr,
  36. // data: {
  37. // estree: parser.parse(exportPropsStr, {
  38. // sourceType: 'module',
  39. // ecmaVersion: 'latest',
  40. // plugins: {
  41. // 'jsx': true
  42. // }
  43. // })
  44. // }
  45. // })
  46. // for (let node of tree.children) {
  47. // if (node.type === 'mdxjsEsm' && new RegExp(`export\\s+default`).test(node.value)) {
  48. // console.log('Found default export')
  49. // return
  50. // }
  51. // }
  52. // if(layout) {
  53. // console.log('Adding layout')
  54. // let importStr = `import ${layout} from '@/layouts/${layout}.js'`
  55. // tree.children.push({
  56. // type: 'mdxjsEsm',
  57. // value: importStr,
  58. // data: {
  59. // estree: parser.parse(importStr, {
  60. // sourceType: 'module',
  61. // ecmaVersion: 'latest'
  62. // })
  63. // }
  64. // })
  65. // let exportStr = `MDXContent.Layout = ${layout}`
  66. // tree.children.push({
  67. // type: 'mdxjsEsm',
  68. // value: exportStr,
  69. // data: {
  70. // estree: parser.parse(exportStr, {
  71. // sourceType: 'module',
  72. // ecmaVersion: 'latest',
  73. // plugins: {
  74. // 'jsx': true
  75. // }
  76. // })
  77. // }
  78. // })
  79. // }
  80. // }
  81. // }
  82. // const preview = () => {
  83. // return (tree, file) => {
  84. // console.log('--------------------------');
  85. // console.log('tree', tree);
  86. // console.log('--------------------------');
  87. // console.log(toMarkdown(tree, {
  88. // extensions: [
  89. // frontmatterToMarkdown(['yaml', 'toml']),
  90. // mdxjsEsmToMarkdown,
  91. // mdxJsxToMarkdown(),
  92. // gfmTableToMarkdown()
  93. // ]
  94. // }))
  95. // console.log('--------------------------');
  96. // }
  97. // }
  98. export const remarkPlugins = [
  99. mdxAnnotations.remark,
  100. remarkMdx,
  101. remarkFrontmatter,
  102. remarkMdxCodeMeta,
  103. [remarkMdxFrontmatter, {
  104. name: 'meta'
  105. }],
  106. remarkParseFrontmatter,
  107. remarkGfm,
  108. [remarkGithub, {
  109. repository: 'https://github.com/tabler/tabler'
  110. }],
  111. remarkMath,
  112. remarkSmartypants,
  113. remarkUnwrapImages,
  114. remarkToc,
  115. // remarkLayout,
  116. ]