Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

heading-atx.mjs 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. var headingAtx = {
  2. name: 'headingAtx',
  3. tokenize: tokenizeHeadingAtx,
  4. resolve: resolveHeadingAtx
  5. }
  6. export default headingAtx
  7. import assert from 'assert'
  8. import codes from '../character/codes.mjs'
  9. import markdownLineEnding from '../character/markdown-line-ending.mjs'
  10. import markdownLineEndingOrSpace from '../character/markdown-line-ending-or-space.mjs'
  11. import markdownSpace from '../character/markdown-space.mjs'
  12. import constants from '../constant/constants.mjs'
  13. import types from '../constant/types.mjs'
  14. import chunkedSplice from '../util/chunked-splice.mjs'
  15. import spaceFactory from './factory-space.mjs'
  16. function resolveHeadingAtx(events, context) {
  17. var contentEnd = events.length - 2
  18. var contentStart = 3
  19. var content
  20. var text
  21. // Prefix whitespace, part of the opening.
  22. if (events[contentStart][1].type === types.whitespace) {
  23. contentStart += 2
  24. }
  25. // Suffix whitespace, part of the closing.
  26. if (
  27. contentEnd - 2 > contentStart &&
  28. events[contentEnd][1].type === types.whitespace
  29. ) {
  30. contentEnd -= 2
  31. }
  32. if (
  33. events[contentEnd][1].type === types.atxHeadingSequence &&
  34. (contentStart === contentEnd - 1 ||
  35. (contentEnd - 4 > contentStart &&
  36. events[contentEnd - 2][1].type === types.whitespace))
  37. ) {
  38. contentEnd -= contentStart + 1 === contentEnd ? 2 : 4
  39. }
  40. if (contentEnd > contentStart) {
  41. content = {
  42. type: types.atxHeadingText,
  43. start: events[contentStart][1].start,
  44. end: events[contentEnd][1].end
  45. }
  46. text = {
  47. type: types.chunkText,
  48. start: events[contentStart][1].start,
  49. end: events[contentEnd][1].end,
  50. contentType: constants.contentTypeText
  51. }
  52. chunkedSplice(events, contentStart, contentEnd - contentStart + 1, [
  53. ['enter', content, context],
  54. ['enter', text, context],
  55. ['exit', text, context],
  56. ['exit', content, context]
  57. ])
  58. }
  59. return events
  60. }
  61. function tokenizeHeadingAtx(effects, ok, nok) {
  62. var self = this
  63. var size = 0
  64. return start
  65. function start(code) {
  66. assert(code === codes.numberSign, 'expected `#`')
  67. effects.enter(types.atxHeading)
  68. effects.enter(types.atxHeadingSequence)
  69. return fenceOpenInside(code)
  70. }
  71. function fenceOpenInside(code) {
  72. if (
  73. code === codes.numberSign &&
  74. size++ < constants.atxHeadingOpeningFenceSizeMax
  75. ) {
  76. effects.consume(code)
  77. return fenceOpenInside
  78. }
  79. if (code === codes.eof || markdownLineEndingOrSpace(code)) {
  80. effects.exit(types.atxHeadingSequence)
  81. return self.interrupt ? ok(code) : headingBreak(code)
  82. }
  83. return nok(code)
  84. }
  85. function headingBreak(code) {
  86. if (code === codes.numberSign) {
  87. effects.enter(types.atxHeadingSequence)
  88. return sequence(code)
  89. }
  90. if (code === codes.eof || markdownLineEnding(code)) {
  91. effects.exit(types.atxHeading)
  92. return ok(code)
  93. }
  94. if (markdownSpace(code)) {
  95. return spaceFactory(effects, headingBreak, types.whitespace)(code)
  96. }
  97. effects.enter(types.atxHeadingText)
  98. return data(code)
  99. }
  100. function sequence(code) {
  101. if (code === codes.numberSign) {
  102. effects.consume(code)
  103. return sequence
  104. }
  105. effects.exit(types.atxHeadingSequence)
  106. return headingBreak(code)
  107. }
  108. function data(code) {
  109. if (
  110. code === codes.eof ||
  111. code === codes.numberSign ||
  112. markdownLineEndingOrSpace(code)
  113. ) {
  114. effects.exit(types.atxHeadingText)
  115. return headingBreak(code)
  116. }
  117. effects.consume(code)
  118. return data
  119. }
  120. }