factory-title.mjs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. export default titleFactory
  2. import assert from 'assert'
  3. import codes from '../character/codes.mjs'
  4. import markdownLineEnding from '../character/markdown-line-ending.mjs'
  5. import constants from '../constant/constants.mjs'
  6. import types from '../constant/types.mjs'
  7. import spaceFactory from './factory-space.mjs'
  8. // eslint-disable-next-line max-params
  9. function titleFactory(effects, ok, nok, type, markerType, stringType) {
  10. var marker
  11. return start
  12. function start(code) {
  13. assert(
  14. code === codes.quotationMark ||
  15. code === codes.apostrophe ||
  16. code === codes.leftParenthesis,
  17. 'expected `"`, `\'`, or `(`'
  18. )
  19. effects.enter(type)
  20. effects.enter(markerType)
  21. effects.consume(code)
  22. effects.exit(markerType)
  23. marker = code === codes.leftParenthesis ? codes.rightParenthesis : code
  24. return atFirstTitleBreak
  25. }
  26. function atFirstTitleBreak(code) {
  27. if (code === marker) {
  28. effects.enter(markerType)
  29. effects.consume(code)
  30. effects.exit(markerType)
  31. effects.exit(type)
  32. return ok
  33. }
  34. effects.enter(stringType)
  35. return atTitleBreak(code)
  36. }
  37. function atTitleBreak(code) {
  38. if (code === marker) {
  39. effects.exit(stringType)
  40. return atFirstTitleBreak(marker)
  41. }
  42. if (code === codes.eof) {
  43. return nok(code)
  44. }
  45. // Note: blank lines can’t exist in content.
  46. if (markdownLineEnding(code)) {
  47. effects.enter(types.lineEnding)
  48. effects.consume(code)
  49. effects.exit(types.lineEnding)
  50. return spaceFactory(effects, atTitleBreak, types.linePrefix)
  51. }
  52. effects.enter(types.chunkString, {contentType: constants.contentTypeString})
  53. return title(code)
  54. }
  55. function title(code) {
  56. if (code === marker || code === codes.eof || markdownLineEnding(code)) {
  57. effects.exit(types.chunkString)
  58. return atTitleBreak(code)
  59. }
  60. effects.consume(code)
  61. return code === codes.backslash ? titleEscape : title
  62. }
  63. function titleEscape(code) {
  64. if (code === marker || code === codes.backslash) {
  65. effects.consume(code)
  66. return title
  67. }
  68. return title(code)
  69. }
  70. }