micromark-extension-directive/lib/factory-name.js
2021-06-08 15:39:19 +02:00

32 lines
688 B
JavaScript

import {asciiAlpha, asciiAlphanumeric} from 'micromark-util-character'
export function factoryName(effects, ok, nok, nameType) {
const self = this
return start
function start(code) {
if (asciiAlpha(code)) {
effects.enter(nameType)
effects.consume(code)
return name
}
return nok(code)
}
function name(code) {
if (
code === 45 /* `-` */ ||
code === 95 /* `_` */ ||
asciiAlphanumeric(code)
) {
effects.consume(code)
return name
}
effects.exit(nameType)
// To do next major: disallow `-` at end of name too, for consistency.
return self.previous === 95 /* `_` */ ? nok(code) : ok(code)
}
}