index.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. * @import {Encoding, Value} from 'micromark-util-types'
  3. */
  4. /**
  5. * @typedef {import('micromark-util-types').Options} Options
  6. */
  7. import { compile } from './lib/compile.js';
  8. import { parse } from './lib/parse.js';
  9. import { postprocess } from './lib/postprocess.js';
  10. import { preprocess } from './lib/preprocess.js';
  11. export { compile } from './lib/compile.js';
  12. export { parse } from './lib/parse.js';
  13. export { postprocess } from './lib/postprocess.js';
  14. export { preprocess } from './lib/preprocess.js';
  15. /**
  16. * Compile markdown to HTML.
  17. *
  18. * > Note: which encodings are supported depends on the engine.
  19. * > For info on Node.js, see:
  20. * > <https://nodejs.org/api/util.html#whatwg-supported-encodings>.
  21. *
  22. * @overload
  23. * @param {Value} value
  24. * Markdown to parse (`string` or `Uint8Array`).
  25. * @param {Encoding | null | undefined} encoding
  26. * Character encoding to understand `value` as when it’s a `Uint8Array`
  27. * (`string`, default: `'utf8'`).
  28. * @param {Options | null | undefined} [options]
  29. * Configuration.
  30. * @returns {string}
  31. * Compiled HTML.
  32. *
  33. * @overload
  34. * @param {Value} value
  35. * Markdown to parse (`string` or `Uint8Array`).
  36. * @param {Options | null | undefined} [options]
  37. * Configuration.
  38. * @returns {string}
  39. * Compiled HTML.
  40. *
  41. * @param {Value} value
  42. * Markdown to parse (`string` or `Uint8Array`).
  43. * @param {Encoding | Options | null | undefined} [encoding]
  44. * Character encoding to understand `value` as when it’s a `Uint8Array`
  45. * (`string`, default: `'utf8'`).
  46. * @param {Options | null | undefined} [options]
  47. * Configuration.
  48. * @returns {string}
  49. * Compiled HTML.
  50. */
  51. export function micromark(value, encoding, options) {
  52. if (typeof encoding !== 'string') {
  53. options = encoding;
  54. encoding = undefined;
  55. }
  56. return compile(options)(postprocess(parse(options).document().write(preprocess()(value, encoding, true))));
  57. }