123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- 'use strict'
-
- var markdownLineEnding = require('../character/markdown-line-ending.js')
- var markdownLineEndingOrSpace = require('../character/markdown-line-ending-or-space.js')
- var prefixSize = require('../util/prefix-size.js')
- var factorySpace = require('./factory-space.js')
-
- var codeFenced = {
- name: 'codeFenced',
- tokenize: tokenizeCodeFenced,
- concrete: true
- }
-
- function tokenizeCodeFenced(effects, ok, nok) {
- var self = this
- var closingFenceConstruct = {
- tokenize: tokenizeClosingFence,
- partial: true
- }
- var initialPrefix = prefixSize(this.events, 'linePrefix')
- var sizeOpen = 0
- var marker
- return start
-
- function start(code) {
- effects.enter('codeFenced')
- effects.enter('codeFencedFence')
- effects.enter('codeFencedFenceSequence')
- marker = code
- return sequenceOpen(code)
- }
-
- function sequenceOpen(code) {
- if (code === marker) {
- effects.consume(code)
- sizeOpen++
- return sequenceOpen
- }
-
- effects.exit('codeFencedFenceSequence')
- return sizeOpen < 3
- ? nok(code)
- : factorySpace(effects, infoOpen, 'whitespace')(code)
- }
-
- function infoOpen(code) {
- if (code === null || markdownLineEnding(code)) {
- return openAfter(code)
- }
-
- effects.enter('codeFencedFenceInfo')
- effects.enter('chunkString', {
- contentType: 'string'
- })
- return info(code)
- }
-
- function info(code) {
- if (code === null || markdownLineEndingOrSpace(code)) {
- effects.exit('chunkString')
- effects.exit('codeFencedFenceInfo')
- return factorySpace(effects, infoAfter, 'whitespace')(code)
- }
-
- if (code === 96 && code === marker) return nok(code)
- effects.consume(code)
- return info
- }
-
- function infoAfter(code) {
- if (code === null || markdownLineEnding(code)) {
- return openAfter(code)
- }
-
- effects.enter('codeFencedFenceMeta')
- effects.enter('chunkString', {
- contentType: 'string'
- })
- return meta(code)
- }
-
- function meta(code) {
- if (code === null || markdownLineEnding(code)) {
- effects.exit('chunkString')
- effects.exit('codeFencedFenceMeta')
- return openAfter(code)
- }
-
- if (code === 96 && code === marker) return nok(code)
- effects.consume(code)
- return meta
- }
-
- function openAfter(code) {
- effects.exit('codeFencedFence')
- return self.interrupt ? ok(code) : content(code)
- }
-
- function content(code) {
- if (code === null) {
- return after(code)
- }
-
- if (markdownLineEnding(code)) {
- effects.enter('lineEnding')
- effects.consume(code)
- effects.exit('lineEnding')
- return effects.attempt(
- closingFenceConstruct,
- after,
- initialPrefix
- ? factorySpace(effects, content, 'linePrefix', initialPrefix + 1)
- : content
- )
- }
-
- effects.enter('codeFlowValue')
- return contentContinue(code)
- }
-
- function contentContinue(code) {
- if (code === null || markdownLineEnding(code)) {
- effects.exit('codeFlowValue')
- return content(code)
- }
-
- effects.consume(code)
- return contentContinue
- }
-
- function after(code) {
- effects.exit('codeFenced')
- return ok(code)
- }
-
- function tokenizeClosingFence(effects, ok, nok) {
- var size = 0
- return factorySpace(
- effects,
- closingSequenceStart,
- 'linePrefix',
- this.parser.constructs.disable.null.indexOf('codeIndented') > -1
- ? undefined
- : 4
- )
-
- function closingSequenceStart(code) {
- effects.enter('codeFencedFence')
- effects.enter('codeFencedFenceSequence')
- return closingSequence(code)
- }
-
- function closingSequence(code) {
- if (code === marker) {
- effects.consume(code)
- size++
- return closingSequence
- }
-
- if (size < sizeOpen) return nok(code)
- effects.exit('codeFencedFenceSequence')
- return factorySpace(effects, closingSequenceEnd, 'whitespace')(code)
- }
-
- function closingSequenceEnd(code) {
- if (code === null || markdownLineEnding(code)) {
- effects.exit('codeFencedFence')
- return ok(code)
- }
-
- return nok(code)
- }
- }
- }
-
- module.exports = codeFenced
|