index.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /**
  2. * @typedef {import('mdast').Root} Root
  3. * @typedef {import('mdast-util-to-markdown').Options} ToMarkdownOptions
  4. * @typedef {import('unified').Compiler<Root, string>} Compiler
  5. * @typedef {import('unified').Processor<undefined, undefined, undefined, Root, string>} Processor
  6. */
  7. /**
  8. * @typedef {Omit<ToMarkdownOptions, 'extensions'>} Options
  9. */
  10. import {toMarkdown} from 'mdast-util-to-markdown'
  11. /**
  12. * Add support for serializing to markdown.
  13. *
  14. * @param {Readonly<Options> | null | undefined} [options]
  15. * Configuration (optional).
  16. * @returns {undefined}
  17. * Nothing.
  18. */
  19. export default function remarkStringify(options) {
  20. /** @type {Processor} */
  21. // @ts-expect-error: TS in JSDoc generates wrong types if `this` is typed regularly.
  22. const self = this
  23. self.compiler = compiler
  24. /**
  25. * @type {Compiler}
  26. */
  27. function compiler(tree) {
  28. return toMarkdown(tree, {
  29. ...self.data('settings'),
  30. ...options,
  31. // Note: this option is not in the readme.
  32. // The goal is for it to be set by plugins on `data` instead of being
  33. // passed by users.
  34. extensions: self.data('toMarkdownExtensions') || []
  35. })
  36. }
  37. }