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.

html.mjs 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. // While micromark is a lexer/tokenizer, the common case of going from markdown
  2. // to html is currently built in as this module, even though the parts can be
  3. // used separately to build ASTs, CSTs, or many other output formats.
  4. //
  5. // Having an HTML compiler built in is useful because it allows us to check for
  6. // compliancy to CommonMark, the de facto norm of markdown, specified in roughly
  7. // 600 input/output cases.
  8. //
  9. // This module has an interface which accepts lists of events instead of the
  10. // whole at once, however, because markdown can’t be truly streaming, we buffer
  11. // events before processing and outputting the final result.
  12. export default compileHtml
  13. import decodeEntity from 'parse-entities/decode-entity.js'
  14. import codes from '../character/codes.mjs'
  15. import assign from '../constant/assign.mjs'
  16. import constants from '../constant/constants.mjs'
  17. import own from '../constant/has-own-property.mjs'
  18. import types from '../constant/types.mjs'
  19. import combineHtmlExtensions from '../util/combine-html-extensions.mjs'
  20. import chunkedPush from '../util/chunked-push.mjs'
  21. import miniflat from '../util/miniflat.mjs'
  22. import normalizeIdentifier from '../util/normalize-identifier.mjs'
  23. import normalizeUri from '../util/normalize-uri.mjs'
  24. import safeFromInt from '../util/safe-from-int.mjs'
  25. // This ensures that certain characters which have special meaning in HTML are
  26. // dealt with.
  27. // Technically, we can skip `>` and `"` in many cases, but CM includes them.
  28. var characterReferences = {'"': 'quot', '&': 'amp', '<': 'lt', '>': 'gt'}
  29. // These two are allowlists of essentially safe protocols for full URLs in
  30. // respectively the `href` (on `<a>`) and `src` (on `<img>`) attributes.
  31. // They are based on what is allowed on GitHub,
  32. // <https://github.com/syntax-tree/hast-util-sanitize/blob/9275b21/lib/github.json#L31>
  33. var protocolHref = /^(https?|ircs?|mailto|xmpp)$/i
  34. var protocolSrc = /^https?$/i
  35. function compileHtml(options) {
  36. // Configuration.
  37. // Includes `htmlExtensions` (an array of extensions), `defaultLineEnding` (a
  38. // preferred EOL), `allowDangerousProtocol` (whether to allow potential
  39. // dangerous protocols), and `allowDangerousHtml` (whether to allow potential
  40. // dangerous HTML).
  41. var settings = options || {}
  42. // Tags is needed because according to markdown, links and emphasis and
  43. // whatnot can exist in images, however, as HTML doesn’t allow content in
  44. // images, the tags are ignored in the `alt` attribute, but the content
  45. // remains.
  46. var tags = true
  47. // An object to track identifiers to media (URLs and titles) defined with
  48. // definitions.
  49. var definitions = {}
  50. // A lot of the handlers need to capture some of the output data, modify it
  51. // somehow, and then deal with it.
  52. // We do that by tracking a stack of buffers, that can be opened (with
  53. // `buffer`) and closed (with `resume`) to access them.
  54. var buffers = [[]]
  55. // As we can have links in images and the other way around, where the deepest
  56. // ones are closed first, we need to track which one we’re in.
  57. var mediaStack = []
  58. // Same for tightness, which is specific to lists.
  59. // We need to track if we’re currently in a tight or loose container.
  60. var tightStack = []
  61. var defaultHandlers = {
  62. enter: {
  63. blockQuote: onenterblockquote,
  64. codeFenced: onentercodefenced,
  65. codeFencedFenceInfo: buffer,
  66. codeFencedFenceMeta: buffer,
  67. codeIndented: onentercodeindented,
  68. codeText: onentercodetext,
  69. content: onentercontent,
  70. definition: onenterdefinition,
  71. definitionDestinationString: onenterdefinitiondestinationstring,
  72. definitionLabelString: buffer,
  73. definitionTitleString: buffer,
  74. emphasis: onenteremphasis,
  75. htmlFlow: onenterhtmlflow,
  76. htmlText: onenterhtml,
  77. image: onenterimage,
  78. label: buffer,
  79. link: onenterlink,
  80. listItemMarker: onenterlistitemmarker,
  81. listItemValue: onenterlistitemvalue,
  82. listOrdered: onenterlistordered,
  83. listUnordered: onenterlistunordered,
  84. paragraph: onenterparagraph,
  85. reference: buffer,
  86. resource: onenterresource,
  87. resourceDestinationString: onenterresourcedestinationstring,
  88. resourceTitleString: buffer,
  89. setextHeading: onentersetextheading,
  90. strong: onenterstrong
  91. },
  92. exit: {
  93. atxHeading: onexitatxheading,
  94. atxHeadingSequence: onexitatxheadingsequence,
  95. autolinkEmail: onexitautolinkemail,
  96. autolinkProtocol: onexitautolinkprotocol,
  97. blockQuote: onexitblockquote,
  98. characterEscapeValue: onexitdata,
  99. characterReferenceMarkerHexadecimal: onexitcharacterreferencemarker,
  100. characterReferenceMarkerNumeric: onexitcharacterreferencemarker,
  101. characterReferenceValue: onexitcharacterreferencevalue,
  102. codeFenced: onexitflowcode,
  103. codeFencedFence: onexitcodefencedfence,
  104. codeFencedFenceInfo: onexitcodefencedfenceinfo,
  105. codeFencedFenceMeta: resume,
  106. codeFlowValue: onexitcodeflowvalue,
  107. codeIndented: onexitflowcode,
  108. codeText: onexitcodetext,
  109. codeTextData: onexitdata,
  110. data: onexitdata,
  111. definition: onexitdefinition,
  112. definitionDestinationString: onexitdefinitiondestinationstring,
  113. definitionLabelString: onexitdefinitionlabelstring,
  114. definitionTitleString: onexitdefinitiontitlestring,
  115. emphasis: onexitemphasis,
  116. hardBreakEscape: onexithardbreak,
  117. hardBreakTrailing: onexithardbreak,
  118. htmlFlow: onexithtml,
  119. htmlFlowData: onexitdata,
  120. htmlText: onexithtml,
  121. htmlTextData: onexitdata,
  122. image: onexitmedia,
  123. label: onexitlabel,
  124. labelText: onexitlabeltext,
  125. lineEnding: onexitlineending,
  126. link: onexitmedia,
  127. listOrdered: onexitlistordered,
  128. listUnordered: onexitlistunordered,
  129. paragraph: onexitparagraph,
  130. reference: resume,
  131. referenceString: onexitreferencestring,
  132. resource: resume,
  133. resourceDestinationString: onexitresourcedestinationstring,
  134. resourceTitleString: onexitresourcetitlestring,
  135. setextHeading: onexitsetextheading,
  136. setextHeadingLineSequence: onexitsetextheadinglinesequence,
  137. setextHeadingText: onexitsetextheadingtext,
  138. strong: onexitstrong,
  139. thematicBreak: onexitthematicbreak
  140. }
  141. }
  142. // Combine the HTML extensions with the default handlers.
  143. // An HTML extension is an object whose fields are either `enter` or `exit`
  144. // (reflecting whether a token is entered or exited).
  145. // The values at such objects are names of tokens mapping to handlers.
  146. // Handlers are called, respectively when a token is opener or closed, with
  147. // that token, and a context as `this`.
  148. var handlers = combineHtmlExtensions(
  149. [defaultHandlers].concat(miniflat(settings.htmlExtensions))
  150. )
  151. // Handlers do often need to keep track of some state.
  152. // That state is provided here as a key-value store (an object).
  153. var data = {tightStack: tightStack}
  154. // The context for handlers references a couple of useful functions.
  155. // In handlers from extensions, those can be accessed at `this`.
  156. // For the handlers here, they can be accessed directly.
  157. var context = {
  158. lineEndingIfNeeded: lineEndingIfNeeded,
  159. options: settings,
  160. encode: encode,
  161. raw: raw,
  162. tag: tag,
  163. buffer: buffer,
  164. resume: resume,
  165. setData: setData,
  166. getData: getData
  167. }
  168. // Generally, micromark copies line endings (`'\r'`, `'\n'`, `'\r\n'`) in the
  169. // markdown document over to the compiled HTML.
  170. // In some cases, such as `> a`, CommonMark requires that extra line endings
  171. // are added: `<blockquote>\n<p>a</p>\n</blockquote>`.
  172. // This variable hold the default line ending when given (or `undefined`),
  173. // and in the latter case will be updated to the first found line ending if
  174. // there is one.
  175. var lineEndingStyle = settings.defaultLineEnding
  176. // Return the function that handles a slice of events.
  177. return compile
  178. // Deal w/ a slice of events.
  179. // Return either the empty string if there’s nothing of note to return, or the
  180. // result when done.
  181. function compile(events) {
  182. // As definitions can come after references, we need to figure out the media
  183. // (urls and titles) defined by them before handling the references.
  184. // So, we do sort of what HTML does: put metadata at the start (in head), and
  185. // then put content after (`body`).
  186. var head = []
  187. var body = []
  188. var index
  189. var start
  190. var listStack
  191. var handler
  192. var result
  193. index = -1
  194. start = 0
  195. listStack = []
  196. while (++index < events.length) {
  197. // Figure out the line ending style used in the document.
  198. if (
  199. !lineEndingStyle &&
  200. (events[index][1].type === types.lineEnding ||
  201. events[index][1].type === types.lineEndingBlank)
  202. ) {
  203. lineEndingStyle = events[index][2].sliceSerialize(events[index][1])
  204. }
  205. // Preprocess lists to infer whether the list is loose or not.
  206. if (
  207. events[index][1].type === types.listOrdered ||
  208. events[index][1].type === types.listUnordered
  209. ) {
  210. if (events[index][0] === 'enter') {
  211. listStack.push(index)
  212. } else {
  213. prepareList(events.slice(listStack.pop(), index))
  214. }
  215. }
  216. // Move definitions to the front.
  217. if (events[index][1].type === types.definition) {
  218. if (events[index][0] === 'enter') {
  219. body = chunkedPush(body, events.slice(start, index))
  220. start = index
  221. } else {
  222. head = chunkedPush(head, events.slice(start, index + 1))
  223. start = index + 1
  224. }
  225. }
  226. }
  227. head = chunkedPush(head, body)
  228. head = chunkedPush(head, events.slice(start))
  229. result = head
  230. index = -1
  231. // Handle the start of the document, if defined.
  232. if (handlers.enter.null) {
  233. handlers.enter.null.call(context)
  234. }
  235. // Handle all events.
  236. while (++index < events.length) {
  237. handler = handlers[result[index][0]]
  238. if (own.call(handler, result[index][1].type)) {
  239. handler[result[index][1].type].call(
  240. assign({sliceSerialize: result[index][2].sliceSerialize}, context),
  241. result[index][1]
  242. )
  243. }
  244. }
  245. // Handle the end of the document, if defined.
  246. if (handlers.exit.null) {
  247. handlers.exit.null.call(context)
  248. }
  249. return buffers[0].join('')
  250. }
  251. // Figure out whether lists are loose or not.
  252. function prepareList(slice) {
  253. var length = slice.length - 1 // Skip close.
  254. var index = 0 // Skip open.
  255. var containerBalance = 0
  256. var loose
  257. var atMarker
  258. var event
  259. while (++index < length) {
  260. event = slice[index]
  261. if (event[1]._container) {
  262. atMarker = undefined
  263. if (event[0] === 'enter') {
  264. containerBalance++
  265. } else {
  266. containerBalance--
  267. }
  268. } else if (event[1].type === types.listItemPrefix) {
  269. if (event[0] === 'exit') {
  270. atMarker = true
  271. }
  272. } else if (event[1].type === types.linePrefix) {
  273. // Ignore
  274. } else if (event[1].type === types.lineEndingBlank) {
  275. if (event[0] === 'enter' && !containerBalance) {
  276. if (atMarker) {
  277. atMarker = undefined
  278. } else {
  279. loose = true
  280. }
  281. }
  282. } else {
  283. atMarker = undefined
  284. }
  285. }
  286. slice[0][1]._loose = loose
  287. }
  288. // Set data into the key-value store.
  289. function setData(key, value) {
  290. data[key] = value
  291. }
  292. // Get data from the key-value store.
  293. function getData(key) {
  294. return data[key]
  295. }
  296. // Capture some of the output data.
  297. function buffer() {
  298. buffers.push([])
  299. }
  300. // Stop capturing and access the output data.
  301. function resume() {
  302. return buffers.pop().join('')
  303. }
  304. // Output (parts of) HTML tags.
  305. function tag(value) {
  306. if (!tags) return
  307. setData('lastWasTag', true)
  308. buffers[buffers.length - 1].push(value)
  309. }
  310. // Output raw data.
  311. function raw(value) {
  312. setData('lastWasTag')
  313. buffers[buffers.length - 1].push(value)
  314. }
  315. // Output an extra line ending.
  316. function lineEnding() {
  317. raw(lineEndingStyle || '\n')
  318. }
  319. // Output an extra line ending if the previous value wasn’t EOF/EOL.
  320. function lineEndingIfNeeded() {
  321. var buffer = buffers[buffers.length - 1]
  322. var slice = buffer[buffer.length - 1]
  323. var previous = slice ? slice.charCodeAt(slice.length - 1) : codes.eof
  324. if (
  325. previous === codes.lf ||
  326. previous === codes.cr ||
  327. previous === codes.eof
  328. ) {
  329. return
  330. }
  331. lineEnding()
  332. }
  333. // Make a value safe for injection in HTML (except w/ `ignoreEncode`).
  334. function encode(value) {
  335. return getData('ignoreEncode') ? value : value.replace(/["&<>]/g, replace)
  336. function replace(value) {
  337. return '&' + characterReferences[value] + ';'
  338. }
  339. }
  340. // Make a value safe for injection as a URL.
  341. // This does encode unsafe characters with percent-encoding, skipping already
  342. // encoded sequences (`normalizeUri`).
  343. // Further unsafe characters are encoded as character references (`encode`).
  344. // Finally, if the URL includes an unknown protocol (such as a dangerous
  345. // example, `javascript:`), the value is ignored.
  346. function url(url, protocol) {
  347. var value = encode(normalizeUri(url || ''))
  348. var colon = value.indexOf(':')
  349. var questionMark = value.indexOf('?')
  350. var numberSign = value.indexOf('#')
  351. var slash = value.indexOf('/')
  352. if (
  353. settings.allowDangerousProtocol ||
  354. // If there is no protocol, it’s relative.
  355. colon < 0 ||
  356. // If the first colon is after a `?`, `#`, or `/`, it’s not a protocol.
  357. (slash > -1 && colon > slash) ||
  358. (questionMark > -1 && colon > questionMark) ||
  359. (numberSign > -1 && colon > numberSign) ||
  360. // It is a protocol, it should be allowed.
  361. protocol.test(value.slice(0, colon))
  362. ) {
  363. return value
  364. }
  365. return ''
  366. }
  367. //
  368. // Handlers.
  369. //
  370. function onenterlistordered(token) {
  371. tightStack.push(!token._loose)
  372. lineEndingIfNeeded()
  373. tag('<ol')
  374. setData('expectFirstItem', true)
  375. }
  376. function onenterlistunordered(token) {
  377. tightStack.push(!token._loose)
  378. lineEndingIfNeeded()
  379. tag('<ul')
  380. setData('expectFirstItem', true)
  381. }
  382. function onenterlistitemvalue(token) {
  383. var value
  384. if (getData('expectFirstItem')) {
  385. value = parseInt(this.sliceSerialize(token), constants.numericBaseDecimal)
  386. if (value !== 1) {
  387. tag(' start="' + encode(String(value)) + '"')
  388. }
  389. }
  390. }
  391. function onenterlistitemmarker() {
  392. if (getData('expectFirstItem')) {
  393. tag('>')
  394. } else {
  395. onexitlistitem()
  396. }
  397. lineEndingIfNeeded()
  398. tag('<li>')
  399. setData('expectFirstItem')
  400. // “Hack” to prevent a line ending from showing up if the item is empty.
  401. setData('lastWasTag')
  402. }
  403. function onexitlistordered() {
  404. onexitlistitem()
  405. tightStack.pop()
  406. lineEnding()
  407. tag('</ol>')
  408. }
  409. function onexitlistunordered() {
  410. onexitlistitem()
  411. tightStack.pop()
  412. lineEnding()
  413. tag('</ul>')
  414. }
  415. function onexitlistitem() {
  416. if (getData('lastWasTag') && !getData('slurpAllLineEndings')) {
  417. lineEndingIfNeeded()
  418. }
  419. tag('</li>')
  420. setData('slurpAllLineEndings')
  421. }
  422. function onenterblockquote() {
  423. tightStack.push(false)
  424. lineEndingIfNeeded()
  425. tag('<blockquote>')
  426. }
  427. function onexitblockquote() {
  428. tightStack.pop()
  429. lineEndingIfNeeded()
  430. tag('</blockquote>')
  431. setData('slurpAllLineEndings')
  432. }
  433. function onenterparagraph() {
  434. if (!tightStack[tightStack.length - 1]) {
  435. lineEndingIfNeeded()
  436. tag('<p>')
  437. }
  438. setData('slurpAllLineEndings')
  439. }
  440. function onexitparagraph() {
  441. if (tightStack[tightStack.length - 1]) {
  442. setData('slurpAllLineEndings', true)
  443. } else {
  444. tag('</p>')
  445. }
  446. }
  447. function onentercodefenced() {
  448. lineEndingIfNeeded()
  449. tag('<pre><code')
  450. setData('fencesCount', 0)
  451. }
  452. function onexitcodefencedfenceinfo() {
  453. var value = resume()
  454. tag(' class="language-' + value + '"')
  455. }
  456. function onexitcodefencedfence() {
  457. if (!getData('fencesCount')) {
  458. tag('>')
  459. setData('fencedCodeInside', true)
  460. setData('slurpOneLineEnding', true)
  461. }
  462. setData('fencesCount', getData('fencesCount') + 1)
  463. }
  464. function onentercodeindented() {
  465. lineEndingIfNeeded()
  466. tag('<pre><code>')
  467. }
  468. function onexitflowcode() {
  469. // Send an extra line feed if we saw data.
  470. if (getData('flowCodeSeenData')) lineEndingIfNeeded()
  471. tag('</code></pre>')
  472. if (getData('fencesCount') < 2) lineEndingIfNeeded()
  473. setData('flowCodeSeenData')
  474. setData('fencesCount')
  475. setData('slurpOneLineEnding')
  476. }
  477. function onenterimage() {
  478. mediaStack.push({image: true})
  479. tags = undefined // Disallow tags.
  480. }
  481. function onenterlink() {
  482. mediaStack.push({})
  483. }
  484. function onexitlabeltext(token) {
  485. mediaStack[mediaStack.length - 1].labelId = this.sliceSerialize(token)
  486. }
  487. function onexitlabel() {
  488. mediaStack[mediaStack.length - 1].label = resume()
  489. }
  490. function onexitreferencestring(token) {
  491. mediaStack[mediaStack.length - 1].referenceId = this.sliceSerialize(token)
  492. }
  493. function onenterresource() {
  494. buffer() // We can have line endings in the resource, ignore them.
  495. mediaStack[mediaStack.length - 1].destination = ''
  496. }
  497. function onenterresourcedestinationstring() {
  498. buffer()
  499. // Ignore encoding the result, as we’ll first percent encode the url and
  500. // encode manually after.
  501. setData('ignoreEncode', true)
  502. }
  503. function onexitresourcedestinationstring() {
  504. mediaStack[mediaStack.length - 1].destination = resume()
  505. setData('ignoreEncode')
  506. }
  507. function onexitresourcetitlestring() {
  508. mediaStack[mediaStack.length - 1].title = resume()
  509. }
  510. function onexitmedia() {
  511. var index = mediaStack.length - 1 // Skip current.
  512. var media = mediaStack[index]
  513. var context =
  514. media.destination === undefined
  515. ? definitions[normalizeIdentifier(media.referenceId || media.labelId)]
  516. : media
  517. tags = true
  518. while (index--) {
  519. if (mediaStack[index].image) {
  520. tags = undefined
  521. break
  522. }
  523. }
  524. if (media.image) {
  525. tag('<img src="' + url(context.destination, protocolSrc) + '" alt="')
  526. raw(media.label)
  527. tag('"')
  528. } else {
  529. tag('<a href="' + url(context.destination, protocolHref) + '"')
  530. }
  531. tag(context.title ? ' title="' + context.title + '"' : '')
  532. if (media.image) {
  533. tag(' />')
  534. } else {
  535. tag('>')
  536. raw(media.label)
  537. tag('</a>')
  538. }
  539. mediaStack.pop()
  540. }
  541. function onenterdefinition() {
  542. buffer()
  543. mediaStack.push({})
  544. }
  545. function onexitdefinitionlabelstring(token) {
  546. // Discard label, use the source content instead.
  547. resume()
  548. mediaStack[mediaStack.length - 1].labelId = this.sliceSerialize(token)
  549. }
  550. function onenterdefinitiondestinationstring() {
  551. buffer()
  552. setData('ignoreEncode', true)
  553. }
  554. function onexitdefinitiondestinationstring() {
  555. mediaStack[mediaStack.length - 1].destination = resume()
  556. setData('ignoreEncode')
  557. }
  558. function onexitdefinitiontitlestring() {
  559. mediaStack[mediaStack.length - 1].title = resume()
  560. }
  561. function onexitdefinition() {
  562. var id = normalizeIdentifier(mediaStack[mediaStack.length - 1].labelId)
  563. resume()
  564. if (!own.call(definitions, id)) {
  565. definitions[id] = mediaStack[mediaStack.length - 1]
  566. }
  567. mediaStack.pop()
  568. }
  569. function onentercontent() {
  570. setData('slurpAllLineEndings', true)
  571. }
  572. function onexitatxheadingsequence(token) {
  573. // Exit for further sequences.
  574. if (getData('headingRank')) return
  575. setData('headingRank', this.sliceSerialize(token).length)
  576. lineEndingIfNeeded()
  577. tag('<h' + getData('headingRank') + '>')
  578. }
  579. function onentersetextheading() {
  580. buffer()
  581. setData('slurpAllLineEndings')
  582. }
  583. function onexitsetextheadingtext() {
  584. setData('slurpAllLineEndings', true)
  585. }
  586. function onexitatxheading() {
  587. tag('</h' + getData('headingRank') + '>')
  588. setData('headingRank')
  589. }
  590. function onexitsetextheadinglinesequence(token) {
  591. setData(
  592. 'headingRank',
  593. this.sliceSerialize(token).charCodeAt(0) === codes.equalsTo ? 1 : 2
  594. )
  595. }
  596. function onexitsetextheading() {
  597. var value = resume()
  598. lineEndingIfNeeded()
  599. tag('<h' + getData('headingRank') + '>')
  600. raw(value)
  601. tag('</h' + getData('headingRank') + '>')
  602. setData('slurpAllLineEndings')
  603. setData('headingRank')
  604. }
  605. function onexitdata(token) {
  606. raw(encode(this.sliceSerialize(token)))
  607. }
  608. function onexitlineending(token) {
  609. if (getData('slurpAllLineEndings')) {
  610. return
  611. }
  612. if (getData('slurpOneLineEnding')) {
  613. setData('slurpOneLineEnding')
  614. return
  615. }
  616. if (getData('inCodeText')) {
  617. raw(' ')
  618. return
  619. }
  620. raw(encode(this.sliceSerialize(token)))
  621. }
  622. function onexitcodeflowvalue(token) {
  623. raw(encode(this.sliceSerialize(token)))
  624. setData('flowCodeSeenData', true)
  625. }
  626. function onexithardbreak() {
  627. tag('<br />')
  628. }
  629. function onenterhtmlflow() {
  630. lineEndingIfNeeded()
  631. onenterhtml()
  632. }
  633. function onexithtml() {
  634. setData('ignoreEncode')
  635. }
  636. function onenterhtml() {
  637. if (settings.allowDangerousHtml) {
  638. setData('ignoreEncode', true)
  639. }
  640. }
  641. function onenteremphasis() {
  642. tag('<em>')
  643. }
  644. function onenterstrong() {
  645. tag('<strong>')
  646. }
  647. function onentercodetext() {
  648. setData('inCodeText', true)
  649. tag('<code>')
  650. }
  651. function onexitcodetext() {
  652. setData('inCodeText')
  653. tag('</code>')
  654. }
  655. function onexitemphasis() {
  656. tag('</em>')
  657. }
  658. function onexitstrong() {
  659. tag('</strong>')
  660. }
  661. function onexitthematicbreak() {
  662. lineEndingIfNeeded()
  663. tag('<hr />')
  664. }
  665. function onexitcharacterreferencemarker(token) {
  666. setData('characterReferenceType', token.type)
  667. }
  668. function onexitcharacterreferencevalue(token) {
  669. var value = this.sliceSerialize(token)
  670. value = getData('characterReferenceType')
  671. ? safeFromInt(
  672. value,
  673. getData('characterReferenceType') ===
  674. types.characterReferenceMarkerNumeric
  675. ? constants.numericBaseDecimal
  676. : constants.numericBaseHexadecimal
  677. )
  678. : decodeEntity(value)
  679. raw(encode(value))
  680. setData('characterReferenceType')
  681. }
  682. function onexitautolinkprotocol(token) {
  683. var uri = this.sliceSerialize(token)
  684. tag('<a href="' + url(uri, protocolHref) + '">')
  685. raw(encode(uri))
  686. tag('</a>')
  687. }
  688. function onexitautolinkemail(token) {
  689. var uri = this.sliceSerialize(token)
  690. tag('<a href="' + url('mailto:' + uri, protocolHref) + '">')
  691. raw(encode(uri))
  692. tag('</a>')
  693. }
  694. }