|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import {markdownLineEnding} from 'micromark-util-character' |
|
|
|
export const codeText = { |
|
name: 'codeText', |
|
tokenize: tokenizeCodeText, |
|
resolve: resolveCodeText, |
|
previous |
|
} |
|
|
|
|
|
|
|
function resolveCodeText(events) { |
|
let tailExitIndex = events.length - 4 |
|
let headEnterIndex = 3 |
|
|
|
let index |
|
|
|
let enter |
|
|
|
|
|
if ( |
|
(events[headEnterIndex][1].type === 'lineEnding' || |
|
events[headEnterIndex][1].type === 'space') && |
|
(events[tailExitIndex][1].type === 'lineEnding' || |
|
events[tailExitIndex][1].type === 'space') |
|
) { |
|
index = headEnterIndex |
|
|
|
|
|
while (++index < tailExitIndex) { |
|
if (events[index][1].type === 'codeTextData') { |
|
|
|
events[headEnterIndex][1].type = 'codeTextPadding' |
|
events[tailExitIndex][1].type = 'codeTextPadding' |
|
headEnterIndex += 2 |
|
tailExitIndex -= 2 |
|
break |
|
} |
|
} |
|
} |
|
|
|
|
|
index = headEnterIndex - 1 |
|
tailExitIndex++ |
|
while (++index <= tailExitIndex) { |
|
if (enter === undefined) { |
|
if (index !== tailExitIndex && events[index][1].type !== 'lineEnding') { |
|
enter = index |
|
} |
|
} else if ( |
|
index === tailExitIndex || |
|
events[index][1].type === 'lineEnding' |
|
) { |
|
events[enter][1].type = 'codeTextData' |
|
if (index !== enter + 2) { |
|
events[enter][1].end = events[index - 1][1].end |
|
events.splice(enter + 2, index - enter - 2) |
|
tailExitIndex -= index - enter - 2 |
|
index = enter + 2 |
|
} |
|
enter = undefined |
|
} |
|
} |
|
return events |
|
} |
|
|
|
|
|
|
|
|
|
|
|
function previous(code) { |
|
|
|
return ( |
|
code !== 96 || |
|
this.events[this.events.length - 1][1].type === 'characterEscape' |
|
) |
|
} |
|
|
|
|
|
|
|
|
|
|
|
function tokenizeCodeText(effects, ok, nok) { |
|
const self = this |
|
let sizeOpen = 0 |
|
|
|
let size |
|
|
|
let token |
|
return start |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function start(code) { |
|
effects.enter('codeText') |
|
effects.enter('codeTextSequence') |
|
return sequenceOpen(code) |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function sequenceOpen(code) { |
|
if (code === 96) { |
|
effects.consume(code) |
|
sizeOpen++ |
|
return sequenceOpen |
|
} |
|
effects.exit('codeTextSequence') |
|
return between(code) |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function between(code) { |
|
|
|
if (code === null) { |
|
return nok(code) |
|
} |
|
|
|
|
|
|
|
|
|
if (code === 32) { |
|
effects.enter('space') |
|
effects.consume(code) |
|
effects.exit('space') |
|
return between |
|
} |
|
|
|
|
|
if (code === 96) { |
|
token = effects.enter('codeTextSequence') |
|
size = 0 |
|
return sequenceClose(code) |
|
} |
|
if (markdownLineEnding(code)) { |
|
effects.enter('lineEnding') |
|
effects.consume(code) |
|
effects.exit('lineEnding') |
|
return between |
|
} |
|
|
|
|
|
effects.enter('codeTextData') |
|
return data(code) |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function data(code) { |
|
if ( |
|
code === null || |
|
code === 32 || |
|
code === 96 || |
|
markdownLineEnding(code) |
|
) { |
|
effects.exit('codeTextData') |
|
return between(code) |
|
} |
|
effects.consume(code) |
|
return data |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function sequenceClose(code) { |
|
|
|
if (code === 96) { |
|
effects.consume(code) |
|
size++ |
|
return sequenceClose |
|
} |
|
|
|
|
|
if (size === sizeOpen) { |
|
effects.exit('codeTextSequence') |
|
effects.exit('codeText') |
|
return ok(code) |
|
} |
|
|
|
|
|
token.type = 'codeTextData' |
|
return data(code) |
|
} |
|
} |
|
|