parse.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /**
  2. * @typedef {import('micromark-util-types').Create} Create
  3. * @typedef {import('micromark-util-types').FullNormalizedExtension} FullNormalizedExtension
  4. * @typedef {import('micromark-util-types').InitialConstruct} InitialConstruct
  5. * @typedef {import('micromark-util-types').ParseContext} ParseContext
  6. * @typedef {import('micromark-util-types').ParseOptions} ParseOptions
  7. */
  8. import {combineExtensions} from 'micromark-util-combine-extensions'
  9. import {content} from './initialize/content.js'
  10. import {document} from './initialize/document.js'
  11. import {flow} from './initialize/flow.js'
  12. import {string, text} from './initialize/text.js'
  13. import {createTokenizer} from './create-tokenizer.js'
  14. import * as defaultConstructs from './constructs.js'
  15. /**
  16. * @param {ParseOptions | null | undefined} [options]
  17. * @returns {ParseContext}
  18. */
  19. export function parse(options) {
  20. const settings = options || {}
  21. const constructs =
  22. /** @type {FullNormalizedExtension} */
  23. combineExtensions([defaultConstructs, ...(settings.extensions || [])])
  24. /** @type {ParseContext} */
  25. const parser = {
  26. defined: [],
  27. lazy: {},
  28. constructs,
  29. content: create(content),
  30. document: create(document),
  31. flow: create(flow),
  32. string: create(string),
  33. text: create(text)
  34. }
  35. return parser
  36. /**
  37. * @param {InitialConstruct} initial
  38. */
  39. function create(initial) {
  40. return creator
  41. /** @type {Create} */
  42. function creator(from) {
  43. return createTokenizer(parser, initial, from)
  44. }
  45. }
  46. }