This commit is contained in:
2024-03-22 03:47:51 +05:30
parent 8bcf3d211e
commit 89819f6fe2
28440 changed files with 3211033 additions and 2 deletions

View File

@@ -0,0 +1,80 @@
import type {Attribute, Directive} from './lib/html.js'
export {directive} from './lib/syntax.js'
export {
directiveHtml,
type Directive,
type Handle,
type HtmlOptions
} from './lib/html.js'
declare module 'micromark-util-types' {
interface TokenTypeMap {
directiveContainer: 'directiveContainer'
directiveContainerAttributes: 'directiveContainerAttributes'
directiveContainerAttributesMarker: 'directiveContainerAttributesMarker'
directiveContainerAttribute: 'directiveContainerAttribute'
directiveContainerAttributeId: 'directiveContainerAttributeId'
directiveContainerAttributeIdValue: 'directiveContainerAttributeIdValue'
directiveContainerAttributeClass: 'directiveContainerAttributeClass'
directiveContainerAttributeClassValue: 'directiveContainerAttributeClassValue'
directiveContainerAttributeName: 'directiveContainerAttributeName'
directiveContainerAttributeInitializerMarker: 'directiveContainerAttributeInitializerMarker'
directiveContainerAttributeValueLiteral: 'directiveContainerAttributeValueLiteral'
directiveContainerAttributeValue: 'directiveContainerAttributeValue'
directiveContainerAttributeValueMarker: 'directiveContainerAttributeValueMarker'
directiveContainerAttributeValueData: 'directiveContainerAttributeValueData'
directiveContainerContent: 'directiveContainerContent'
directiveContainerFence: 'directiveContainerFence'
directiveContainerLabel: 'directiveContainerLabel'
directiveContainerLabelMarker: 'directiveContainerLabelMarker'
directiveContainerLabelString: 'directiveContainerLabelString'
directiveContainerName: 'directiveContainerName'
directiveContainerSequence: 'directiveContainerSequence'
directiveLeaf: 'directiveLeaf'
directiveLeafAttributes: 'directiveLeafAttributes'
directiveLeafAttributesMarker: 'directiveLeafAttributesMarker'
directiveLeafAttribute: 'directiveLeafAttribute'
directiveLeafAttributeId: 'directiveLeafAttributeId'
directiveLeafAttributeIdValue: 'directiveLeafAttributeIdValue'
directiveLeafAttributeClass: 'directiveLeafAttributeClass'
directiveLeafAttributeClassValue: 'directiveLeafAttributeClassValue'
directiveLeafAttributeName: 'directiveLeafAttributeName'
directiveLeafAttributeInitializerMarker: 'directiveLeafAttributeInitializerMarker'
directiveLeafAttributeValueLiteral: 'directiveLeafAttributeValueLiteral'
directiveLeafAttributeValue: 'directiveLeafAttributeValue'
directiveLeafAttributeValueMarker: 'directiveLeafAttributeValueMarker'
directiveLeafAttributeValueData: 'directiveLeafAttributeValueData'
directiveLeafLabel: 'directiveLeafLabel'
directiveLeafLabelMarker: 'directiveLeafLabelMarker'
directiveLeafLabelString: 'directiveLeafLabelString'
directiveLeafName: 'directiveLeafName'
directiveLeafSequence: 'directiveLeafSequence'
directiveText: 'directiveText'
directiveTextAttributes: 'directiveTextAttributes'
directiveTextAttributesMarker: 'directiveTextAttributesMarker'
directiveTextAttribute: 'directiveTextAttribute'
directiveTextAttributeId: 'directiveTextAttributeId'
directiveTextAttributeIdValue: 'directiveTextAttributeIdValue'
directiveTextAttributeClass: 'directiveTextAttributeClass'
directiveTextAttributeClassValue: 'directiveTextAttributeClassValue'
directiveTextAttributeName: 'directiveTextAttributeName'
directiveTextAttributeInitializerMarker: 'directiveTextAttributeInitializerMarker'
directiveTextAttributeValueLiteral: 'directiveTextAttributeValueLiteral'
directiveTextAttributeValue: 'directiveTextAttributeValue'
directiveTextAttributeValueMarker: 'directiveTextAttributeValueMarker'
directiveTextAttributeValueData: 'directiveTextAttributeValueData'
directiveTextLabel: 'directiveTextLabel'
directiveTextLabelMarker: 'directiveTextLabelMarker'
directiveTextLabelString: 'directiveTextLabelString'
directiveTextMarker: 'directiveTextMarker'
directiveTextName: 'directiveTextName'
}
interface CompileData {
directiveAttributes?: Attribute[]
directiveStack?: Directive[]
}
}

View File

@@ -0,0 +1,3 @@
// Note: more types exported from `index.d.ts`.
export {directive} from './lib/syntax.js'
export {directiveHtml} from './lib/html.js'

View File

@@ -0,0 +1,7 @@
/** @type {Construct} */
export const directiveContainer: Construct
export type Construct = import('micromark-util-types').Construct
export type State = import('micromark-util-types').State
export type Token = import('micromark-util-types').Token
export type TokenizeContext = import('micromark-util-types').TokenizeContext
export type Tokenizer = import('micromark-util-types').Tokenizer

View File

@@ -0,0 +1,308 @@
/**
* @typedef {import('micromark-util-types').Construct} Construct
* @typedef {import('micromark-util-types').State} State
* @typedef {import('micromark-util-types').Token} Token
* @typedef {import('micromark-util-types').TokenizeContext} TokenizeContext
* @typedef {import('micromark-util-types').Tokenizer} Tokenizer
*/
import {ok as assert} from 'devlop'
import {factorySpace} from 'micromark-factory-space'
import {markdownLineEnding} from 'micromark-util-character'
import {codes, constants, types} from 'micromark-util-symbol'
import {factoryAttributes} from './factory-attributes.js'
import {factoryLabel} from './factory-label.js'
import {factoryName} from './factory-name.js'
/** @type {Construct} */
export const directiveContainer = {
tokenize: tokenizeDirectiveContainer,
concrete: true
}
const label = {tokenize: tokenizeLabel, partial: true}
const attributes = {tokenize: tokenizeAttributes, partial: true}
const nonLazyLine = {tokenize: tokenizeNonLazyLine, partial: true}
/**
* @this {TokenizeContext}
* @type {Tokenizer}
*/
function tokenizeDirectiveContainer(effects, ok, nok) {
const self = this
const tail = self.events[self.events.length - 1]
const initialSize =
tail && tail[1].type === types.linePrefix
? tail[2].sliceSerialize(tail[1], true).length
: 0
let sizeOpen = 0
/** @type {Token} */
let previous
return start
/** @type {State} */
function start(code) {
assert(code === codes.colon, 'expected `:`')
effects.enter('directiveContainer')
effects.enter('directiveContainerFence')
effects.enter('directiveContainerSequence')
return sequenceOpen(code)
}
/** @type {State} */
function sequenceOpen(code) {
if (code === codes.colon) {
effects.consume(code)
sizeOpen++
return sequenceOpen
}
if (sizeOpen < constants.codeFencedSequenceSizeMin) {
return nok(code)
}
effects.exit('directiveContainerSequence')
return factoryName.call(
self,
effects,
afterName,
nok,
'directiveContainerName'
)(code)
}
/** @type {State} */
function afterName(code) {
return code === codes.leftSquareBracket
? effects.attempt(label, afterLabel, afterLabel)(code)
: afterLabel(code)
}
/** @type {State} */
function afterLabel(code) {
return code === codes.leftCurlyBrace
? effects.attempt(attributes, afterAttributes, afterAttributes)(code)
: afterAttributes(code)
}
/** @type {State} */
function afterAttributes(code) {
return factorySpace(effects, openAfter, types.whitespace)(code)
}
/** @type {State} */
function openAfter(code) {
effects.exit('directiveContainerFence')
if (code === codes.eof) {
return afterOpening(code)
}
if (markdownLineEnding(code)) {
if (self.interrupt) {
return ok(code)
}
return effects.attempt(nonLazyLine, contentStart, afterOpening)(code)
}
return nok(code)
}
/** @type {State} */
function afterOpening(code) {
effects.exit('directiveContainer')
return ok(code)
}
/** @type {State} */
function contentStart(code) {
if (code === codes.eof) {
effects.exit('directiveContainer')
return ok(code)
}
effects.enter('directiveContainerContent')
return lineStart(code)
}
/** @type {State} */
function lineStart(code) {
if (code === codes.eof) {
return after(code)
}
return effects.attempt(
{tokenize: tokenizeClosingFence, partial: true},
after,
initialSize
? factorySpace(effects, chunkStart, types.linePrefix, initialSize + 1)
: chunkStart
)(code)
}
/** @type {State} */
function chunkStart(code) {
if (code === codes.eof) {
return after(code)
}
const token = effects.enter(types.chunkDocument, {
contentType: constants.contentTypeDocument,
previous
})
if (previous) previous.next = token
previous = token
return contentContinue(code)
}
/** @type {State} */
function contentContinue(code) {
if (code === codes.eof) {
const t = effects.exit(types.chunkDocument)
self.parser.lazy[t.start.line] = false
return after(code)
}
if (markdownLineEnding(code)) {
return effects.check(nonLazyLine, nonLazyLineAfter, lineAfter)(code)
}
effects.consume(code)
return contentContinue
}
/** @type {State} */
function nonLazyLineAfter(code) {
effects.consume(code)
const t = effects.exit(types.chunkDocument)
self.parser.lazy[t.start.line] = false
return lineStart
}
/** @type {State} */
function lineAfter(code) {
const t = effects.exit(types.chunkDocument)
self.parser.lazy[t.start.line] = false
return after(code)
}
/** @type {State} */
function after(code) {
effects.exit('directiveContainerContent')
effects.exit('directiveContainer')
return ok(code)
}
/**
* @this {TokenizeContext}
* @type {Tokenizer}
*/
function tokenizeClosingFence(effects, ok, nok) {
let size = 0
return factorySpace(
effects,
closingPrefixAfter,
types.linePrefix,
constants.tabSize
)
/** @type {State} */
function closingPrefixAfter(code) {
effects.enter('directiveContainerFence')
effects.enter('directiveContainerSequence')
return closingSequence(code)
}
/** @type {State} */
function closingSequence(code) {
if (code === codes.colon) {
effects.consume(code)
size++
return closingSequence
}
if (size < sizeOpen) return nok(code)
effects.exit('directiveContainerSequence')
return factorySpace(effects, closingSequenceEnd, types.whitespace)(code)
}
/** @type {State} */
function closingSequenceEnd(code) {
if (code === codes.eof || markdownLineEnding(code)) {
effects.exit('directiveContainerFence')
return ok(code)
}
return nok(code)
}
}
}
/**
* @this {TokenizeContext}
* @type {Tokenizer}
*/
function tokenizeLabel(effects, ok, nok) {
// Always a `[`
return factoryLabel(
effects,
ok,
nok,
'directiveContainerLabel',
'directiveContainerLabelMarker',
'directiveContainerLabelString',
true
)
}
/**
* @this {TokenizeContext}
* @type {Tokenizer}
*/
function tokenizeAttributes(effects, ok, nok) {
// Always a `{`
return factoryAttributes(
effects,
ok,
nok,
'directiveContainerAttributes',
'directiveContainerAttributesMarker',
'directiveContainerAttribute',
'directiveContainerAttributeId',
'directiveContainerAttributeClass',
'directiveContainerAttributeName',
'directiveContainerAttributeInitializerMarker',
'directiveContainerAttributeValueLiteral',
'directiveContainerAttributeValue',
'directiveContainerAttributeValueMarker',
'directiveContainerAttributeValueData',
true
)
}
/**
* @this {TokenizeContext}
* @type {Tokenizer}
*/
function tokenizeNonLazyLine(effects, ok, nok) {
const self = this
return start
/** @type {State} */
function start(code) {
assert(markdownLineEnding(code), 'expected eol')
effects.enter(types.lineEnding)
effects.consume(code)
effects.exit(types.lineEnding)
return lineStart
}
/** @type {State} */
function lineStart(code) {
return self.parser.lazy[self.now().line] ? nok(code) : ok(code)
}
}

View File

@@ -0,0 +1,6 @@
/** @type {Construct} */
export const directiveLeaf: Construct
export type Construct = import('micromark-util-types').Construct
export type State = import('micromark-util-types').State
export type TokenizeContext = import('micromark-util-types').TokenizeContext
export type Tokenizer = import('micromark-util-types').Tokenizer

View File

@@ -0,0 +1,127 @@
/**
* @typedef {import('micromark-util-types').Construct} Construct
* @typedef {import('micromark-util-types').State} State
* @typedef {import('micromark-util-types').TokenizeContext} TokenizeContext
* @typedef {import('micromark-util-types').Tokenizer} Tokenizer
*/
import {ok as assert} from 'devlop'
import {factorySpace} from 'micromark-factory-space'
import {markdownLineEnding} from 'micromark-util-character'
import {codes, types} from 'micromark-util-symbol'
import {factoryAttributes} from './factory-attributes.js'
import {factoryLabel} from './factory-label.js'
import {factoryName} from './factory-name.js'
/** @type {Construct} */
export const directiveLeaf = {tokenize: tokenizeDirectiveLeaf}
const label = {tokenize: tokenizeLabel, partial: true}
const attributes = {tokenize: tokenizeAttributes, partial: true}
/**
* @this {TokenizeContext}
* @type {Tokenizer}
*/
function tokenizeDirectiveLeaf(effects, ok, nok) {
const self = this
return start
/** @type {State} */
function start(code) {
assert(code === codes.colon, 'expected `:`')
effects.enter('directiveLeaf')
effects.enter('directiveLeafSequence')
effects.consume(code)
return inStart
}
/** @type {State} */
function inStart(code) {
if (code === codes.colon) {
effects.consume(code)
effects.exit('directiveLeafSequence')
return factoryName.call(
self,
effects,
afterName,
nok,
'directiveLeafName'
)
}
return nok(code)
}
/** @type {State} */
function afterName(code) {
return code === codes.leftSquareBracket
? effects.attempt(label, afterLabel, afterLabel)(code)
: afterLabel(code)
}
/** @type {State} */
function afterLabel(code) {
return code === codes.leftCurlyBrace
? effects.attempt(attributes, afterAttributes, afterAttributes)(code)
: afterAttributes(code)
}
/** @type {State} */
function afterAttributes(code) {
return factorySpace(effects, end, types.whitespace)(code)
}
/** @type {State} */
function end(code) {
if (code === codes.eof || markdownLineEnding(code)) {
effects.exit('directiveLeaf')
return ok(code)
}
return nok(code)
}
}
/**
* @this {TokenizeContext}
* @type {Tokenizer}
*/
function tokenizeLabel(effects, ok, nok) {
// Always a `[`
return factoryLabel(
effects,
ok,
nok,
'directiveLeafLabel',
'directiveLeafLabelMarker',
'directiveLeafLabelString',
true
)
}
/**
* @this {TokenizeContext}
* @type {Tokenizer}
*/
function tokenizeAttributes(effects, ok, nok) {
// Always a `{`
return factoryAttributes(
effects,
ok,
nok,
'directiveLeafAttributes',
'directiveLeafAttributesMarker',
'directiveLeafAttribute',
'directiveLeafAttributeId',
'directiveLeafAttributeClass',
'directiveLeafAttributeName',
'directiveLeafAttributeInitializerMarker',
'directiveLeafAttributeValueLiteral',
'directiveLeafAttributeValue',
'directiveLeafAttributeValueMarker',
'directiveLeafAttributeValueData',
true
)
}

View File

@@ -0,0 +1,7 @@
/** @type {Construct} */
export const directiveText: Construct
export type Construct = import('micromark-util-types').Construct
export type Previous = import('micromark-util-types').Previous
export type State = import('micromark-util-types').State
export type TokenizeContext = import('micromark-util-types').TokenizeContext
export type Tokenizer = import('micromark-util-types').Tokenizer

View File

@@ -0,0 +1,117 @@
/**
* @typedef {import('micromark-util-types').Construct} Construct
* @typedef {import('micromark-util-types').Previous} Previous
* @typedef {import('micromark-util-types').State} State
* @typedef {import('micromark-util-types').TokenizeContext} TokenizeContext
* @typedef {import('micromark-util-types').Tokenizer} Tokenizer
*/
import {ok as assert} from 'devlop'
import {codes, types} from 'micromark-util-symbol'
import {factoryAttributes} from './factory-attributes.js'
import {factoryLabel} from './factory-label.js'
import {factoryName} from './factory-name.js'
/** @type {Construct} */
export const directiveText = {
tokenize: tokenizeDirectiveText,
previous
}
const label = {tokenize: tokenizeLabel, partial: true}
const attributes = {tokenize: tokenizeAttributes, partial: true}
/**
* @this {TokenizeContext}
* @type {Previous}
*/
function previous(code) {
// If there is a previous code, there will always be a tail.
return (
code !== codes.colon ||
this.events[this.events.length - 1][1].type === types.characterEscape
)
}
/**
* @this {TokenizeContext}
* @type {Tokenizer}
*/
function tokenizeDirectiveText(effects, ok, nok) {
const self = this
return start
/** @type {State} */
function start(code) {
assert(code === codes.colon, 'expected `:`')
assert(previous.call(self, self.previous), 'expected correct previous')
effects.enter('directiveText')
effects.enter('directiveTextMarker')
effects.consume(code)
effects.exit('directiveTextMarker')
return factoryName.call(self, effects, afterName, nok, 'directiveTextName')
}
/** @type {State} */
function afterName(code) {
return code === codes.colon
? nok(code)
: code === codes.leftSquareBracket
? effects.attempt(label, afterLabel, afterLabel)(code)
: afterLabel(code)
}
/** @type {State} */
function afterLabel(code) {
return code === codes.leftCurlyBrace
? effects.attempt(attributes, afterAttributes, afterAttributes)(code)
: afterAttributes(code)
}
/** @type {State} */
function afterAttributes(code) {
effects.exit('directiveText')
return ok(code)
}
}
/**
* @this {TokenizeContext}
* @type {Tokenizer}
*/
function tokenizeLabel(effects, ok, nok) {
// Always a `[`
return factoryLabel(
effects,
ok,
nok,
'directiveTextLabel',
'directiveTextLabelMarker',
'directiveTextLabelString'
)
}
/**
* @this {TokenizeContext}
* @type {Tokenizer}
*/
function tokenizeAttributes(effects, ok, nok) {
// Always a `{`
return factoryAttributes(
effects,
ok,
nok,
'directiveTextAttributes',
'directiveTextAttributesMarker',
'directiveTextAttribute',
'directiveTextAttributeId',
'directiveTextAttributeClass',
'directiveTextAttributeName',
'directiveTextAttributeInitializerMarker',
'directiveTextAttributeValueLiteral',
'directiveTextAttributeValue',
'directiveTextAttributeValueMarker',
'directiveTextAttributeValueData'
)
}

View File

@@ -0,0 +1,40 @@
/**
* @param {Effects} effects
* @param {State} ok
* @param {State} nok
* @param {TokenType} attributesType
* @param {TokenType} attributesMarkerType
* @param {TokenType} attributeType
* @param {TokenType} attributeIdType
* @param {TokenType} attributeClassType
* @param {TokenType} attributeNameType
* @param {TokenType} attributeInitializerType
* @param {TokenType} attributeValueLiteralType
* @param {TokenType} attributeValueType
* @param {TokenType} attributeValueMarker
* @param {TokenType} attributeValueData
* @param {boolean | undefined} [disallowEol=false]
*/
export function factoryAttributes(
effects: Effects,
ok: State,
nok: State,
attributesType: TokenType,
attributesMarkerType: TokenType,
attributeType: TokenType,
attributeIdType: TokenType,
attributeClassType: TokenType,
attributeNameType: TokenType,
attributeInitializerType: TokenType,
attributeValueLiteralType: TokenType,
attributeValueType: TokenType,
attributeValueMarker: TokenType,
attributeValueData: TokenType,
disallowEol?: boolean | undefined
): (
code: import('micromark-util-types').Code
) => import('micromark-util-types').State | undefined
export type Code = import('micromark-util-types').Code
export type Effects = import('micromark-util-types').Effects
export type State = import('micromark-util-types').State
export type TokenType = import('micromark-util-types').TokenType

View File

@@ -0,0 +1,341 @@
/**
* @typedef {import('micromark-util-types').Code} Code
* @typedef {import('micromark-util-types').Effects} Effects
* @typedef {import('micromark-util-types').State} State
* @typedef {import('micromark-util-types').TokenType} TokenType
*/
import {ok as assert} from 'devlop'
import {factorySpace} from 'micromark-factory-space'
import {factoryWhitespace} from 'micromark-factory-whitespace'
import {
asciiAlpha,
asciiAlphanumeric,
markdownLineEnding,
markdownLineEndingOrSpace,
markdownSpace
} from 'micromark-util-character'
import {codes, types} from 'micromark-util-symbol'
/**
* @param {Effects} effects
* @param {State} ok
* @param {State} nok
* @param {TokenType} attributesType
* @param {TokenType} attributesMarkerType
* @param {TokenType} attributeType
* @param {TokenType} attributeIdType
* @param {TokenType} attributeClassType
* @param {TokenType} attributeNameType
* @param {TokenType} attributeInitializerType
* @param {TokenType} attributeValueLiteralType
* @param {TokenType} attributeValueType
* @param {TokenType} attributeValueMarker
* @param {TokenType} attributeValueData
* @param {boolean | undefined} [disallowEol=false]
*/
export function factoryAttributes(
effects,
ok,
nok,
attributesType,
attributesMarkerType,
attributeType,
attributeIdType,
attributeClassType,
attributeNameType,
attributeInitializerType,
attributeValueLiteralType,
attributeValueType,
attributeValueMarker,
attributeValueData,
disallowEol
) {
/** @type {TokenType} */
let type
/** @type {Code | undefined} */
let marker
return start
/** @type {State} */
function start(code) {
assert(code === codes.leftCurlyBrace, 'expected `{`')
effects.enter(attributesType)
effects.enter(attributesMarkerType)
effects.consume(code)
effects.exit(attributesMarkerType)
return between
}
/** @type {State} */
function between(code) {
if (code === codes.numberSign) {
type = attributeIdType
return shortcutStart(code)
}
if (code === codes.dot) {
type = attributeClassType
return shortcutStart(code)
}
if (code === codes.colon || code === codes.underscore || asciiAlpha(code)) {
effects.enter(attributeType)
effects.enter(attributeNameType)
effects.consume(code)
return name
}
if (disallowEol && markdownSpace(code)) {
return factorySpace(effects, between, types.whitespace)(code)
}
if (!disallowEol && markdownLineEndingOrSpace(code)) {
return factoryWhitespace(effects, between)(code)
}
return end(code)
}
/** @type {State} */
function shortcutStart(code) {
// Assume its registered.
const markerType = /** @type {TokenType} */ (type + 'Marker')
effects.enter(attributeType)
effects.enter(type)
effects.enter(markerType)
effects.consume(code)
effects.exit(markerType)
return shortcutStartAfter
}
/** @type {State} */
function shortcutStartAfter(code) {
if (
code === codes.eof ||
code === codes.quotationMark ||
code === codes.numberSign ||
code === codes.apostrophe ||
code === codes.dot ||
code === codes.lessThan ||
code === codes.equalsTo ||
code === codes.greaterThan ||
code === codes.graveAccent ||
code === codes.rightCurlyBrace ||
markdownLineEndingOrSpace(code)
) {
return nok(code)
}
// Assume its registered.
const valueType = /** @type {TokenType} */ (type + 'Value')
effects.enter(valueType)
effects.consume(code)
return shortcut
}
/** @type {State} */
function shortcut(code) {
if (
code === codes.eof ||
code === codes.quotationMark ||
code === codes.apostrophe ||
code === codes.lessThan ||
code === codes.equalsTo ||
code === codes.greaterThan ||
code === codes.graveAccent
) {
return nok(code)
}
if (
code === codes.numberSign ||
code === codes.dot ||
code === codes.rightCurlyBrace ||
markdownLineEndingOrSpace(code)
) {
// Assume its registered.
const valueType = /** @type {TokenType} */ (type + 'Value')
effects.exit(valueType)
effects.exit(type)
effects.exit(attributeType)
return between(code)
}
effects.consume(code)
return shortcut
}
/** @type {State} */
function name(code) {
if (
code === codes.dash ||
code === codes.dot ||
code === codes.colon ||
code === codes.underscore ||
asciiAlphanumeric(code)
) {
effects.consume(code)
return name
}
effects.exit(attributeNameType)
if (disallowEol && markdownSpace(code)) {
return factorySpace(effects, nameAfter, types.whitespace)(code)
}
if (!disallowEol && markdownLineEndingOrSpace(code)) {
return factoryWhitespace(effects, nameAfter)(code)
}
return nameAfter(code)
}
/** @type {State} */
function nameAfter(code) {
if (code === codes.equalsTo) {
effects.enter(attributeInitializerType)
effects.consume(code)
effects.exit(attributeInitializerType)
return valueBefore
}
// Attribute w/o value.
effects.exit(attributeType)
return between(code)
}
/** @type {State} */
function valueBefore(code) {
if (
code === codes.eof ||
code === codes.lessThan ||
code === codes.equalsTo ||
code === codes.greaterThan ||
code === codes.graveAccent ||
code === codes.rightCurlyBrace ||
(disallowEol && markdownLineEnding(code))
) {
return nok(code)
}
if (code === codes.quotationMark || code === codes.apostrophe) {
effects.enter(attributeValueLiteralType)
effects.enter(attributeValueMarker)
effects.consume(code)
effects.exit(attributeValueMarker)
marker = code
return valueQuotedStart
}
if (disallowEol && markdownSpace(code)) {
return factorySpace(effects, valueBefore, types.whitespace)(code)
}
if (!disallowEol && markdownLineEndingOrSpace(code)) {
return factoryWhitespace(effects, valueBefore)(code)
}
effects.enter(attributeValueType)
effects.enter(attributeValueData)
effects.consume(code)
marker = undefined
return valueUnquoted
}
/** @type {State} */
function valueUnquoted(code) {
if (
code === codes.eof ||
code === codes.quotationMark ||
code === codes.apostrophe ||
code === codes.lessThan ||
code === codes.equalsTo ||
code === codes.greaterThan ||
code === codes.graveAccent
) {
return nok(code)
}
if (code === codes.rightCurlyBrace || markdownLineEndingOrSpace(code)) {
effects.exit(attributeValueData)
effects.exit(attributeValueType)
effects.exit(attributeType)
return between(code)
}
effects.consume(code)
return valueUnquoted
}
/** @type {State} */
function valueQuotedStart(code) {
if (code === marker) {
effects.enter(attributeValueMarker)
effects.consume(code)
effects.exit(attributeValueMarker)
effects.exit(attributeValueLiteralType)
effects.exit(attributeType)
return valueQuotedAfter
}
effects.enter(attributeValueType)
return valueQuotedBetween(code)
}
/** @type {State} */
function valueQuotedBetween(code) {
if (code === marker) {
effects.exit(attributeValueType)
return valueQuotedStart(code)
}
if (code === codes.eof) {
return nok(code)
}
// Note: blank lines cant exist in content.
if (markdownLineEnding(code)) {
return disallowEol
? nok(code)
: factoryWhitespace(effects, valueQuotedBetween)(code)
}
effects.enter(attributeValueData)
effects.consume(code)
return valueQuoted
}
/** @type {State} */
function valueQuoted(code) {
if (code === marker || code === codes.eof || markdownLineEnding(code)) {
effects.exit(attributeValueData)
return valueQuotedBetween(code)
}
effects.consume(code)
return valueQuoted
}
/** @type {State} */
function valueQuotedAfter(code) {
return code === codes.rightCurlyBrace || markdownLineEndingOrSpace(code)
? between(code)
: end(code)
}
/** @type {State} */
function end(code) {
if (code === codes.rightCurlyBrace) {
effects.enter(attributesMarkerType)
effects.consume(code)
effects.exit(attributesMarkerType)
effects.exit(attributesType)
return ok
}
return nok(code)
}
}

View File

@@ -0,0 +1,24 @@
/**
* @param {Effects} effects
* @param {State} ok
* @param {State} nok
* @param {TokenType} type
* @param {TokenType} markerType
* @param {TokenType} stringType
* @param {boolean | undefined} [disallowEol=false]
*/
export function factoryLabel(
effects: Effects,
ok: State,
nok: State,
type: TokenType,
markerType: TokenType,
stringType: TokenType,
disallowEol?: boolean | undefined
): (
code: import('micromark-util-types').Code
) => import('micromark-util-types').State | undefined
export type Effects = import('micromark-util-types').Effects
export type State = import('micromark-util-types').State
export type Token = import('micromark-util-types').Token
export type TokenType = import('micromark-util-types').TokenType

View File

@@ -0,0 +1,137 @@
/**
* @typedef {import('micromark-util-types').Effects} Effects
* @typedef {import('micromark-util-types').State} State
* @typedef {import('micromark-util-types').Token} Token
* @typedef {import('micromark-util-types').TokenType} TokenType
*/
import {ok as assert} from 'devlop'
import {markdownLineEnding} from 'micromark-util-character'
import {codes, constants, types} from 'micromark-util-symbol'
// This is a fork of:
// <https://github.com/micromark/micromark/tree/main/packages/micromark-factory-label>
// to allow empty labels, balanced brackets (such as for nested directives),
// text instead of strings, and optionally disallows EOLs.
/**
* @param {Effects} effects
* @param {State} ok
* @param {State} nok
* @param {TokenType} type
* @param {TokenType} markerType
* @param {TokenType} stringType
* @param {boolean | undefined} [disallowEol=false]
*/
export function factoryLabel(
effects,
ok,
nok,
type,
markerType,
stringType,
disallowEol
) {
let size = 0
let balance = 0
/** @type {Token | undefined} */
let previous
return start
/** @type {State} */
function start(code) {
assert(code === codes.leftSquareBracket, 'expected `[`')
effects.enter(type)
effects.enter(markerType)
effects.consume(code)
effects.exit(markerType)
return afterStart
}
/** @type {State} */
function afterStart(code) {
if (code === codes.rightSquareBracket) {
effects.enter(markerType)
effects.consume(code)
effects.exit(markerType)
effects.exit(type)
return ok
}
effects.enter(stringType)
return lineStart(code)
}
/** @type {State} */
function lineStart(code) {
if (code === codes.rightSquareBracket && !balance) {
return atClosingBrace(code)
}
const token = effects.enter(types.chunkText, {
contentType: constants.contentTypeText,
previous
})
if (previous) previous.next = token
previous = token
return data(code)
}
/** @type {State} */
function data(code) {
if (code === codes.eof || size > constants.linkReferenceSizeMax) {
return nok(code)
}
if (
code === codes.leftSquareBracket &&
++balance > constants.linkResourceDestinationBalanceMax
) {
return nok(code)
}
if (code === codes.rightSquareBracket && !balance--) {
effects.exit(types.chunkText)
return atClosingBrace(code)
}
if (markdownLineEnding(code)) {
if (disallowEol) {
return nok(code)
}
effects.consume(code)
effects.exit(types.chunkText)
return lineStart
}
effects.consume(code)
return code === codes.backslash ? dataEscape : data
}
/** @type {State} */
function dataEscape(code) {
if (
code === codes.leftSquareBracket ||
code === codes.backslash ||
code === codes.rightSquareBracket
) {
effects.consume(code)
size++
return data
}
return data(code)
}
/** @type {State} */
function atClosingBrace(code) {
effects.exit(stringType)
effects.enter(markerType)
effects.consume(code)
effects.exit(markerType)
effects.exit(type)
return ok
}
}

View File

@@ -0,0 +1,20 @@
/**
* @this {TokenizeContext}
* @param {Effects} effects
* @param {State} ok
* @param {State} nok
* @param {TokenType} type
*/
export function factoryName(
this: import('micromark-util-types').TokenizeContext,
effects: Effects,
ok: State,
nok: State,
type: TokenType
): (
code: import('micromark-util-types').Code
) => import('micromark-util-types').State | undefined
export type Effects = import('micromark-util-types').Effects
export type State = import('micromark-util-types').State
export type TokenizeContext = import('micromark-util-types').TokenizeContext
export type TokenType = import('micromark-util-types').TokenType

View File

@@ -0,0 +1,50 @@
/**
* @typedef {import('micromark-util-types').Effects} Effects
* @typedef {import('micromark-util-types').State} State
* @typedef {import('micromark-util-types').TokenizeContext} TokenizeContext
* @typedef {import('micromark-util-types').TokenType} TokenType
*/
import {asciiAlpha, asciiAlphanumeric} from 'micromark-util-character'
import {codes} from 'micromark-util-symbol'
/**
* @this {TokenizeContext}
* @param {Effects} effects
* @param {State} ok
* @param {State} nok
* @param {TokenType} type
*/
export function factoryName(effects, ok, nok, type) {
const self = this
return start
/** @type {State} */
function start(code) {
if (asciiAlpha(code)) {
effects.enter(type)
effects.consume(code)
return name
}
return nok(code)
}
/** @type {State} */
function name(code) {
if (
code === codes.dash ||
code === codes.underscore ||
asciiAlphanumeric(code)
) {
effects.consume(code)
return name
}
effects.exit(type)
return self.previous === codes.dash || self.previous === codes.underscore
? nok(code)
: ok(code)
}
}

View File

@@ -0,0 +1,70 @@
/**
* Create an extension for `micromark` to support directives when serializing
* to HTML.
*
* @param {HtmlOptions | null | undefined} [options={}]
* Configuration (default: `{}`).
* @returns {HtmlExtension}
* Extension for `micromark` that can be passed in `htmlExtensions`, to
* support directives when serializing to HTML.
*/
export function directiveHtml(
options?: HtmlOptions | null | undefined
): HtmlExtension
export type CompileContext = import('micromark-util-types').CompileContext
export type _Handle = import('micromark-util-types').Handle
export type HtmlExtension = import('micromark-util-types').HtmlExtension
/**
* Internal tuple representing an attribute.
*/
export type Attribute = [string, string]
/**
* Configuration.
*
* > 👉 **Note**: the special field `'*'` can be used to specify a fallback
* > handle to handle all otherwise unhandled directives.
*/
export type HtmlOptions = Record<string, Handle>
/**
* Handle a directive.
*/
export type Handle = (
this: CompileContext,
directive: Directive
) => boolean | undefined
/**
* Structure representing a directive.
*/
export type Directive = {
/**
* Kind.
*/
type: DirectiveType
/**
* Name of directive.
*/
name: string
/**
* Compiled HTML content that was in `[brackets]`.
*/
label?: string | undefined
/**
* Object w/ HTML attributes.
*/
attributes?: Record<string, string> | undefined
/**
* Compiled HTML content inside container directive.
*/
content?: string | undefined
/**
* Private :)
*/
_fenceCount?: number | undefined
}
/**
* Kind.
*/
export type DirectiveType =
| 'containerDirective'
| 'leafDirective'
| 'textDirective'

View File

@@ -0,0 +1,308 @@
/**
* @typedef {import('micromark-util-types').CompileContext} CompileContext
* @typedef {import('micromark-util-types').Handle} _Handle
* @typedef {import('micromark-util-types').HtmlExtension} HtmlExtension
*/
/**
* @typedef {[string, string]} Attribute
* Internal tuple representing an attribute.
*/
/**
* @typedef {Record<string, Handle>} HtmlOptions
* Configuration.
*
* > 👉 **Note**: the special field `'*'` can be used to specify a fallback
* > handle to handle all otherwise unhandled directives.
*
* @callback Handle
* Handle a directive.
* @param {CompileContext} this
* Current context.
* @param {Directive} directive
* Directive.
* @returns {boolean | undefined}
* Signal whether the directive was handled.
*
* Yield `false` to let the fallback (a special handle for `'*'`) handle it.
*
* @typedef Directive
* Structure representing a directive.
* @property {DirectiveType} type
* Kind.
* @property {string} name
* Name of directive.
* @property {string | undefined} [label]
* Compiled HTML content that was in `[brackets]`.
* @property {Record<string, string> | undefined} [attributes]
* Object w/ HTML attributes.
* @property {string | undefined} [content]
* Compiled HTML content inside container directive.
* @property {number | undefined} [_fenceCount]
* Private :)
*
* @typedef {'containerDirective' | 'leafDirective' | 'textDirective'} DirectiveType
* Kind.
*/
import {ok as assert} from 'devlop'
import {parseEntities} from 'parse-entities'
const own = {}.hasOwnProperty
/**
* Create an extension for `micromark` to support directives when serializing
* to HTML.
*
* @param {HtmlOptions | null | undefined} [options={}]
* Configuration (default: `{}`).
* @returns {HtmlExtension}
* Extension for `micromark` that can be passed in `htmlExtensions`, to
* support directives when serializing to HTML.
*/
export function directiveHtml(options) {
const options_ = options || {}
return {
enter: {
directiveContainer() {
enter.call(this, 'containerDirective')
},
directiveContainerAttributes: enterAttributes,
directiveContainerLabel: enterLabel,
directiveContainerContent() {
this.buffer()
},
directiveLeaf() {
enter.call(this, 'leafDirective')
},
directiveLeafAttributes: enterAttributes,
directiveLeafLabel: enterLabel,
directiveText() {
enter.call(this, 'textDirective')
},
directiveTextAttributes: enterAttributes,
directiveTextLabel: enterLabel
},
exit: {
directiveContainer: exit,
directiveContainerAttributeClassValue: exitAttributeClassValue,
directiveContainerAttributeIdValue: exitAttributeIdValue,
directiveContainerAttributeName: exitAttributeName,
directiveContainerAttributeValue: exitAttributeValue,
directiveContainerAttributes: exitAttributes,
directiveContainerContent: exitContainerContent,
directiveContainerFence: exitContainerFence,
directiveContainerLabel: exitLabel,
directiveContainerName: exitName,
directiveLeaf: exit,
directiveLeafAttributeClassValue: exitAttributeClassValue,
directiveLeafAttributeIdValue: exitAttributeIdValue,
directiveLeafAttributeName: exitAttributeName,
directiveLeafAttributeValue: exitAttributeValue,
directiveLeafAttributes: exitAttributes,
directiveLeafLabel: exitLabel,
directiveLeafName: exitName,
directiveText: exit,
directiveTextAttributeClassValue: exitAttributeClassValue,
directiveTextAttributeIdValue: exitAttributeIdValue,
directiveTextAttributeName: exitAttributeName,
directiveTextAttributeValue: exitAttributeValue,
directiveTextAttributes: exitAttributes,
directiveTextLabel: exitLabel,
directiveTextName: exitName
}
}
/**
* @this {CompileContext}
* @param {DirectiveType} type
*/
function enter(type) {
let stack = this.getData('directiveStack')
if (!stack) this.setData('directiveStack', (stack = []))
stack.push({type, name: ''})
}
/**
* @this {CompileContext}
* @type {_Handle}
*/
function exitName(token) {
const stack = this.getData('directiveStack')
assert(stack, 'expected directive stack')
stack[stack.length - 1].name = this.sliceSerialize(token)
}
/**
* @this {CompileContext}
* @type {_Handle}
*/
function enterLabel() {
this.buffer()
}
/**
* @this {CompileContext}
* @type {_Handle}
*/
function exitLabel() {
const data = this.resume()
const stack = this.getData('directiveStack')
assert(stack, 'expected directive stack')
stack[stack.length - 1].label = data
}
/**
* @this {CompileContext}
* @type {_Handle}
*/
function enterAttributes() {
this.buffer()
this.setData('directiveAttributes', [])
}
/**
* @this {CompileContext}
* @type {_Handle}
*/
function exitAttributeIdValue(token) {
const attributes = this.getData('directiveAttributes')
assert(attributes, 'expected attributes')
attributes.push([
'id',
parseEntities(this.sliceSerialize(token), {
attribute: true
})
])
}
/**
* @this {CompileContext}
* @type {_Handle}
*/
function exitAttributeClassValue(token) {
const attributes = this.getData('directiveAttributes')
assert(attributes, 'expected attributes')
attributes.push([
'class',
parseEntities(this.sliceSerialize(token), {
attribute: true
})
])
}
/**
* @this {CompileContext}
* @type {_Handle}
*/
function exitAttributeName(token) {
// Attribute names in CommonMark are significantly limited, so character
// references cant exist.
const attributes = this.getData('directiveAttributes')
assert(attributes, 'expected attributes')
attributes.push([this.sliceSerialize(token), ''])
}
/**
* @this {CompileContext}
* @type {_Handle}
*/
function exitAttributeValue(token) {
const attributes = this.getData('directiveAttributes')
assert(attributes, 'expected attributes')
attributes[attributes.length - 1][1] = parseEntities(
this.sliceSerialize(token),
{attribute: true}
)
}
/**
* @this {CompileContext}
* @type {_Handle}
*/
function exitAttributes() {
const stack = this.getData('directiveStack')
assert(stack, 'expected directive stack')
const attributes = this.getData('directiveAttributes')
assert(attributes, 'expected attributes')
/** @type {Directive['attributes']} */
const cleaned = {}
let index = -1
while (++index < attributes.length) {
const attribute = attributes[index]
if (attribute[0] === 'class' && cleaned.class) {
cleaned.class += ' ' + attribute[1]
} else {
cleaned[attribute[0]] = attribute[1]
}
}
this.resume()
this.setData('directiveAttributes')
stack[stack.length - 1].attributes = cleaned
}
/**
* @this {CompileContext}
* @type {_Handle}
*/
function exitContainerContent() {
const data = this.resume()
const stack = this.getData('directiveStack')
assert(stack, 'expected directive stack')
stack[stack.length - 1].content = data
}
/**
* @this {CompileContext}
* @type {_Handle}
*/
function exitContainerFence() {
const stack = this.getData('directiveStack')
assert(stack, 'expected directive stack')
const directive = stack[stack.length - 1]
if (!directive._fenceCount) directive._fenceCount = 0
directive._fenceCount++
if (directive._fenceCount === 1) this.setData('slurpOneLineEnding', true)
}
/**
* @this {CompileContext}
* @type {_Handle}
*/
function exit() {
const stack = this.getData('directiveStack')
assert(stack, 'expected directive stack')
const directive = stack.pop()
assert(directive, 'expected directive')
/** @type {boolean | undefined} */
let found
/** @type {boolean | undefined} */
let result
assert(directive.name, 'expected `name`')
if (own.call(options_, directive.name)) {
result = options_[directive.name].call(this, directive)
found = result !== false
}
if (!found && own.call(options_, '*')) {
result = options_['*'].call(this, directive)
found = result !== false
}
if (!found && directive.type !== 'textDirective') {
this.setData('slurpOneLineEnding', true)
}
}
}

View File

@@ -0,0 +1,9 @@
/**
* Create an extension for `micromark` to enable directive syntax.
*
* @returns {Extension}
* Extension for `micromark` that can be passed in `extensions`, to
* enable directive syntax.
*/
export function directive(): Extension
export type Extension = import('micromark-util-types').Extension

View File

@@ -0,0 +1,22 @@
/**
* @typedef {import('micromark-util-types').Extension} Extension
*/
import {codes} from 'micromark-util-symbol'
import {directiveContainer} from './directive-container.js'
import {directiveLeaf} from './directive-leaf.js'
import {directiveText} from './directive-text.js'
/**
* Create an extension for `micromark` to enable directive syntax.
*
* @returns {Extension}
* Extension for `micromark` that can be passed in `extensions`, to
* enable directive syntax.
*/
export function directive() {
return {
text: {[codes.colon]: directiveText},
flow: {[codes.colon]: [directiveContainer, directiveLeaf]}
}
}