diff --git a/src/from-markdown.ts b/src/from-markdown.ts index 9a7d945..b9396c6 100644 --- a/src/from-markdown.ts +++ b/src/from-markdown.ts @@ -4,7 +4,7 @@ * Version: 2.1.0 * License: MIT (https://github.com/syntax-tree/mdast-util-directive/blob/main/license) */ -import type { Token } from 'micromark-util-types' +import type { Token } from './micromark-extension/types' import { parseEntities } from 'parse-entities' import { kebabCase } from 'scule' diff --git a/src/micromark-extension/factory-attributes.ts b/src/micromark-extension/factory-attributes.ts index e3c9c9e..b0dff47 100644 --- a/src/micromark-extension/factory-attributes.ts +++ b/src/micromark-extension/factory-attributes.ts @@ -1,4 +1,4 @@ -import type { Effects, State } from 'micromark-util-types' +import type { Effects, State, TokenTypeMap } from './types' import { factorySpace } from 'micromark-factory-space' import { factoryWhitespace } from 'micromark-factory-whitespace' @@ -15,20 +15,20 @@ export default function createAttributes ( effects: Effects, ok: State, nok: State, - attributesType: string, - attributesMarkerType: string, - attributeType: string, - attributeIdType: string, - attributeClassType: string, - attributeNameType: string, - attributeInitializerType: string, - attributeValueLiteralType: string, - attributeValueType: string, - attributeValueMarker: string, - attributeValueData: string, + attributesType: keyof TokenTypeMap, + attributesMarkerType: keyof TokenTypeMap, + attributeType: keyof TokenTypeMap, + attributeIdType: keyof TokenTypeMap, + attributeClassType: keyof TokenTypeMap, + attributeNameType: keyof TokenTypeMap, + attributeInitializerType: keyof TokenTypeMap, + attributeValueLiteralType: keyof TokenTypeMap, + attributeValueType: keyof TokenTypeMap, + attributeValueMarker: keyof TokenTypeMap, + attributeValueData: keyof TokenTypeMap, disallowEol?: boolean ) { - let type: string + let type: keyof TokenTypeMap let marker: number | undefined return start @@ -74,9 +74,9 @@ export default function createAttributes ( function shortcutStart (code: number) { effects.enter(attributeType) effects.enter(type) - effects.enter(type + 'Marker') + effects.enter(type + 'Marker' as keyof TokenTypeMap) effects.consume(code) - effects.exit(type + 'Marker') + effects.exit(type + 'Marker' as keyof TokenTypeMap) return shortcutStartAfter } @@ -97,7 +97,7 @@ export default function createAttributes ( return nok(code) } - effects.enter(type + 'Value') + effects.enter(type + 'Value' as keyof TokenTypeMap) effects.consume(code) return shortcut } @@ -116,7 +116,7 @@ export default function createAttributes ( } if (code === Codes.hash || code === Codes.dot || code === Codes.closingCurlyBracket || markdownLineEndingOrSpace(code)) { - effects.exit(type + 'Value') + effects.exit(type + 'Value' as keyof TokenTypeMap) effects.exit(type) effects.exit(attributeType) return between(code) diff --git a/src/micromark-extension/factory-label.ts b/src/micromark-extension/factory-label.ts index bb4186e..98e6227 100644 --- a/src/micromark-extension/factory-label.ts +++ b/src/micromark-extension/factory-label.ts @@ -1,4 +1,4 @@ -import type { Effects, State } from 'micromark-util-types' +import type { Effects, State, TokenTypeMap } from './types' import { markdownLineEnding } from 'micromark-util-character' import { Codes } from './constants' @@ -11,9 +11,9 @@ export default function createLabel ( effects: Effects, ok: State, nok: State, - type: string, - markerType: string, - stringType: string, + type: keyof TokenTypeMap, + markerType: keyof TokenTypeMap, + stringType: keyof TokenTypeMap, disallowEol?: boolean ) { let size = 0 diff --git a/src/micromark-extension/factory-name.ts b/src/micromark-extension/factory-name.ts index 9263cfd..1a96704 100644 --- a/src/micromark-extension/factory-name.ts +++ b/src/micromark-extension/factory-name.ts @@ -1,14 +1,14 @@ -import type { Effects, State, Code, TokenizeContext } from 'micromark-util-types' +import type { Effects, State, Code, TokenizeContext, TokenTypeMap } from './types' import { asciiAlpha, asciiAlphanumeric } from 'micromark-util-character' import { Codes } from './constants' -export default function createName (this: TokenizeContext, effects: Effects, ok: State, nok: State, nameType: string) { +export default function createName (this: TokenizeContext, effects: Effects, ok: State, nok: State, nameType: keyof TokenTypeMap) { // eslint-disable-next-line @typescript-eslint/no-this-alias const self = this return start - function start (code: Code): State | void { + function start (code: Code): State | undefined { if (asciiAlpha(code)) { effects.enter(nameType) effects.consume(code) @@ -18,7 +18,7 @@ export default function createName (this: TokenizeContext, effects: Effects, ok: return nok(code) } - function name (code: Code): State | void { + function name (code: Code): State { if (code === Codes.dash || code === Codes.underscore || asciiAlphanumeric(code)) { effects.consume(code) return name @@ -26,6 +26,6 @@ export default function createName (this: TokenizeContext, effects: Effects, ok: effects.exit(nameType) // To do next major: disallow `-` at end of name too, for consistency. - return self.previous === Codes.underscore ? nok(code) : ok(code) + return self.previous === Codes.underscore ? nok(code)! : ok(code)! } } diff --git a/src/micromark-extension/tokenize-attribute.ts b/src/micromark-extension/tokenize-attribute.ts index 3e07e46..bd8aff3 100644 --- a/src/micromark-extension/tokenize-attribute.ts +++ b/src/micromark-extension/tokenize-attribute.ts @@ -1,4 +1,4 @@ -import type { Effects, State, Code, TokenizeContext } from 'micromark-util-types' +import type { Effects, State, Code, TokenizeContext } from './types' import { markdownLineEnding } from 'micromark-util-character' import { Codes } from './constants' import createAttributes from './factory-attributes' diff --git a/src/micromark-extension/tokenize-binding.ts b/src/micromark-extension/tokenize-binding.ts index 4aab7cd..a126504 100644 --- a/src/micromark-extension/tokenize-binding.ts +++ b/src/micromark-extension/tokenize-binding.ts @@ -1,4 +1,4 @@ -import type { Effects, State, Code, TokenizeContext } from 'micromark-util-types' +import type { Effects, State, Code, TokenizeContext } from './types' import { Codes } from './constants' function attempClose (this: TokenizeContext, effects: Effects, ok: State, nok: State) { @@ -38,7 +38,7 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat return secondBracket } - function secondBracket (code: Code): void | State { + function secondBracket (code: Code): undefined | State { if (code !== Codes.openingCurlyBracket) { return nok(code) } @@ -49,7 +49,7 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat return content } - function content (code: Code): void | State { + function content (code: Code): undefined | State { if (code === Codes.closingCurlyBracket) { return effects.attempt({ tokenize: attempClose, partial: true }, close, (code) => { effects.consume(code) @@ -61,7 +61,7 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat return content } - function close (code: Code): void | State { + function close (code: Code): undefined | State { return ok(code) } } diff --git a/src/micromark-extension/tokenize-container-indented.ts b/src/micromark-extension/tokenize-container-indented.ts index 6c94fca..d461ad4 100644 --- a/src/micromark-extension/tokenize-container-indented.ts +++ b/src/micromark-extension/tokenize-container-indented.ts @@ -1,4 +1,4 @@ -import type { Effects, State, TokenizeContext, Code } from 'micromark-util-types' +import type { Effects, State, TokenizeContext, Code } from './types' import { codeFenced } from 'micromark-core-commonmark' import { factorySpace } from 'micromark-factory-space' import { prefixSize } from './utils' diff --git a/src/micromark-extension/tokenize-container-suger.ts b/src/micromark-extension/tokenize-container-suger.ts index 8ff6435..b6483f2 100644 --- a/src/micromark-extension/tokenize-container-suger.ts +++ b/src/micromark-extension/tokenize-container-suger.ts @@ -1,4 +1,4 @@ -import type { Effects, State, TokenizeContext, Code } from 'micromark-util-types' +import type { Effects, State, TokenizeContext, Code } from './types' import { markdownLineEnding } from 'micromark-util-character' import { factorySpace } from 'micromark-factory-space' import componentContainer from './tokenize-inline' diff --git a/src/micromark-extension/tokenize-container.ts b/src/micromark-extension/tokenize-container.ts index 0d423c2..8911b08 100644 --- a/src/micromark-extension/tokenize-container.ts +++ b/src/micromark-extension/tokenize-container.ts @@ -1,4 +1,4 @@ -import type { Effects, State, Code, TokenizeContext } from 'micromark-util-types' +import type { Effects, State, Code, TokenizeContext } from './types' import { factorySpace } from 'micromark-factory-space' import { markdownLineEnding, asciiAlpha, markdownSpace } from 'micromark-util-character' import { linePrefixSize, tokenizeCodeFence, useTokenState } from './utils' @@ -33,7 +33,7 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat */ return start - function start (code: Code): State | void { + function start (code: Code): State | undefined { /* istanbul ignore if - handled by mm */ if (code !== Codes.colon) { throw new Error('expected `:`') } effects.enter('componentContainer') @@ -48,7 +48,7 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat let revertSectionState: () => void return closingPrefixAfter - function closingPrefixAfter (code: Code): State | void { + function closingPrefixAfter (code: Code): State | undefined { sectionIndentSize = linePrefixSize(self.events) // Close section @@ -58,7 +58,7 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat return closingSectionSequence(code) } - function closingSectionSequence (code: Code): State | void { + function closingSectionSequence (code: Code): State | undefined { if (code === slotSeparatorCode) { effects.consume(code) size++ @@ -88,7 +88,7 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat } } - function sectionOpen (code: Code): void | State { + function sectionOpen (code: Code): undefined | State { // Open new Section section.enter(effects) @@ -100,7 +100,7 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat return sectionTitle(code) } - function sectionTitle (code: Code): State | void { + function sectionTitle (code: Code): State | undefined { if (markdownLineEnding(code)) { effects.exit('componentContainerSectionTitle') return factorySpace(effects, lineStart, 'linePrefix', 4)(code) @@ -109,7 +109,7 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat return sectionTitle } - function sequenceOpen (code: Code): State | void { + function sequenceOpen (code: Code): State | undefined { if (code === Codes.colon) { effects.consume(code) sizeOpen++ @@ -124,23 +124,23 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat return createName.call(self, effects, afterName, nok, 'componentContainerName')(code) } - function afterName (code: Code): State | void { + function afterName (code: Code): State | undefined { return code === Codes.openingSquareBracket ? effects.attempt(label, afterLabel, afterLabel)(code) : afterLabel(code) } - function afterLabel (code: Code): State | void { + function afterLabel (code: Code): State | undefined { return code === Codes.openingCurlyBracket ? effects.attempt(attributes, afterAttributes, afterAttributes)(code) : afterAttributes(code) } - function afterAttributes (code: Code): State | void { + function afterAttributes (code: Code): State | undefined { return factorySpace(effects, openAfter, 'whitespace')(code) } - function openAfter (code: Code): State | void { + function openAfter (code: Code): State | undefined { effects.exit('componentContainerFence') if (code === null) { @@ -159,7 +159,7 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat return nok(code) } - function contentStart (code: Code): State | void { + function contentStart (code: Code): State | undefined { if (code === null) { effects.exit('componentContainer') return ok(code) @@ -173,7 +173,7 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat return lineStart(code) } - function lineStartAfterPrefix (code: Code): State | void { + function lineStartAfterPrefix (code: Code): State | undefined { if (code === null) { return after(code) } @@ -217,7 +217,7 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat return chunkStart(code) } - function lineStart (code: Code): State | void { + function lineStart (code: Code): State | undefined { if (code === null) { return after(code) } @@ -227,7 +227,7 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat : lineStartAfterPrefix(code) } - function chunkStart (code: Code): State | void { + function chunkStart (code: Code): State | undefined { if (code === null) { return after(code) } @@ -245,7 +245,7 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat return contentContinue(code) } - function contentContinue (code: Code): State | void { + function contentContinue (code: Code): State | undefined { if (code === null) { effects.exit('chunkDocument') return after(code) @@ -261,7 +261,7 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat return contentContinue } - function after (code: Code): State | void { + function after (code: Code): State | undefined { // Close section section.exit(effects) effects.exit('componentContainerContent') @@ -274,13 +274,13 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat return factorySpace(effects, closingPrefixAfter, 'linePrefix', 4) - function closingPrefixAfter (code: Code): State | void { + function closingPrefixAfter (code: Code): State | undefined { effects.enter('componentContainerFence') effects.enter('componentContainerSequence') return closingSequence(code) } - function closingSequence (code: Code): State | void { + function closingSequence (code: Code): State | undefined { if (code === Codes.colon) { effects.consume(code) size++ @@ -300,7 +300,7 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat return factorySpace(effects, closingSequenceEnd, 'whitespace')(code) } - function closingSequenceEnd (code: Code): State | void { + function closingSequenceEnd (code: Code): State | undefined { if (code === null || markdownLineEnding(code)) { effects.exit('componentContainerFence') return ok(code) diff --git a/src/micromark-extension/tokenize-frontmatter.ts b/src/micromark-extension/tokenize-frontmatter.ts index 09e3930..6a11100 100644 --- a/src/micromark-extension/tokenize-frontmatter.ts +++ b/src/micromark-extension/tokenize-frontmatter.ts @@ -1,4 +1,4 @@ -import type { Effects, State, TokenizeContext, Code } from 'micromark-util-types' +import type { Effects, State, TokenizeContext, Code } from './types' import { factorySpace } from 'micromark-factory-space' import { markdownLineEnding, markdownSpace } from 'micromark-util-character' import { Codes, SectionSequenceSize } from './constants' @@ -27,7 +27,7 @@ export function tokenizeFrontMatter ( return closingPrefixAfter - function dataLineFirstSpaces (code: Code): State | void { + function dataLineFirstSpaces (code: Code): State | undefined { if (markdownSpace(code)) { effects.consume(code) sectionIndentSize += 1 @@ -37,7 +37,7 @@ export function tokenizeFrontMatter ( return closingPrefixAfter(code) } - function closingPrefixAfter (code: Code): State | void { + function closingPrefixAfter (code: Code): State | undefined { if (markdownSpace(code)) { effects.enter('space') return dataLineFirstSpaces(code) @@ -49,7 +49,7 @@ export function tokenizeFrontMatter ( return closingSectionSequence(code) } - function closingSectionSequence (code: Code): State | void { + function closingSectionSequence (code: Code): State | undefined { if (code === Codes.dash || markdownSpace(code)) { effects.consume(code) size++ @@ -69,7 +69,7 @@ export function tokenizeFrontMatter ( /** * Enter data section */ - function dataSectionOpen (code: Code): State | void { + function dataSectionOpen (code: Code): State | undefined { effects.enter('componentContainerDataSection') return effects.attempt({ tokenize: tokenizeDataSection as any, @@ -80,7 +80,7 @@ export function tokenizeFrontMatter ( /** * Data section line */ - function dataChunkStart (code: Code): State | void { + function dataChunkStart (code: Code): State | undefined { if (code === null) { effects.exit('componentContainerDataSection') effects.exit('componentContainer') @@ -100,7 +100,7 @@ export function tokenizeFrontMatter ( /** * Data section content */ - function dataContentContinue (code: Code): State | void { + function dataContentContinue (code: Code): State | undefined { if (code === null) { effects.exit('chunkDocument') effects.exit('componentContainerDataSection') @@ -124,7 +124,7 @@ export function tokenizeFrontMatter ( /** * Exit data section */ - function dataSectionClose (code: Code): State | void { + function dataSectionClose (code: Code): State | undefined { effects.exit('componentContainerDataSection') return factorySpace(effects, next, 'whitespace')(code) } diff --git a/src/micromark-extension/tokenize-span.ts b/src/micromark-extension/tokenize-span.ts index 1f3035c..3288c13 100644 --- a/src/micromark-extension/tokenize-span.ts +++ b/src/micromark-extension/tokenize-span.ts @@ -15,7 +15,7 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat return start - function start (code: Code): void | State { + function start (code: Code): State | undefined { if (code !== Codes.openingSquareBracket) { throw new Error('expected `[`') } @@ -36,12 +36,12 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat return effects.check(doubleBracketCheck, nok, attemptLabel)(code) } - function attemptLabel (code: Code): void | State { + function attemptLabel (code: Code): State | undefined { effects.enter('textSpan') return effects.attempt(label, exit as State, nok)(code) } - function exit (code: Code): void | State { + function exit (code: Code): State | undefined { // Prevent conflict with link syntax if (code === Codes.openingParentheses || code === Codes.openingSquareBracket) { return nok(code) @@ -54,7 +54,7 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat return exitOK(code) } - function exitOK (code: Code): void | State { + function exitOK (code: Code): State | undefined { effects.exit('textSpan') return ok(code) } @@ -74,13 +74,13 @@ export default { function checkGfmTaskCheckbox (effects: Effects, ok: State, nok: State) { return enter - function enter (code: Code): void | State { + function enter (code: Code): State | undefined { effects.enter('formGfmTaskCheckbox') effects.consume(code) return check } - function check (code: Code): void | State { + function check (code: Code): State | undefined { if (markdownSpace(code)) { effects.consume(code) return check @@ -102,13 +102,13 @@ function checkGfmTaskCheckbox (effects: Effects, ok: State, nok: State) { function checkDoubleBracket (effects: Effects, ok: State, nok: State) { return enter - function enter (code: Code): void | State { + function enter (code: Code): State | undefined { effects.enter('doubleBracket') effects.consume(code) return check } - function check (code: Code): void | State { + function check (code: Code): State | undefined { if (code !== Codes.openingSquareBracket) { return nok(code) } diff --git a/src/micromark-extension/types.ts b/src/micromark-extension/types.ts new file mode 100644 index 0000000..587a840 --- /dev/null +++ b/src/micromark-extension/types.ts @@ -0,0 +1,64 @@ +export type { Effects, State, Code, TokenizeContext, TokenTypeMap } from 'micromark-util-types' + +declare module 'micromark-util-types' { + interface TokenTypeMap { + componentContainer: 'componentContainer', + componentContainerName: 'componentContainerName', + componentContainerFence: 'componentContainerFence', + componentContainerSequence: 'componentContainerSequence', + componentContainerSection: 'componentContainerSection', + componentContainerSectionSequence: 'componentContainerSectionSequence', + componentContainerSectionTitle: 'componentContainerSectionTitle', + componentContainerContent: 'componentContainerContent', + + componentContainerLabel: 'componentContainerLabel', + componentContainerLabelMarker: 'componentContainerLabelMarker', + componentContainerLabelString: 'componentContainerLabelString', + + componentContainerDataSection: 'componentContainerDataSection', + + componentContainerAttributes: 'componentContainerAttributes', + componentContainerAttributesMarker: 'componentContainerAttributesMarker', + componentContainerAttribute: 'componentContainerAttribute', + componentContainerAttributeId: 'componentContainerAttributeId', + componentContainerAttributeClass: 'componentContainerAttributeClass', + componentContainerAttributeName: 'componentContainerAttributeName', + componentContainerAttributeInitializerMarker: 'componentContainerAttributeInitializerMarker', + componentContainerAttributeValueLiteral: 'componentContainerAttributeValueLiteral', + componentContainerAttributeValue: 'componentContainerAttributeValue', + componentContainerAttributeValueMarker: 'componentContainerAttributeValueMarker', + componentContainerAttributeValueData: 'componentContainerAttributeValueData', + + bindingContent: 'bindingContent', + bindingFence: 'bindingFence', + + + + // Component Text + componentText: 'componentText', + componentTextMarker: 'componentTextMarker', + componentTextName: 'componentTextName', + componentTextLabel: 'componentTextLabel', + componentTextLabelMarker: 'componentTextLabelMarker', + componentTextLabelString: 'componentTextLabelString', + + componentTextAttributes: 'componentTextAttributes', + componentTextAttributesMarker: 'componentTextAttributesMarker', + componentTextAttribute: 'componentTextAttribute', + componentTextAttributeId: 'componentTextAttributeId', + componentTextAttributeClass: 'componentTextAttributeClass', + componentTextAttributeName: 'componentTextAttributeName', + componentTextAttributeInitializerMarker: 'componentTextAttributeInitializerMarker', + componentTextAttributeValueLiteral: 'componentTextAttributeValueLiteral', + componentTextAttributeValue: 'componentTextAttributeValue', + componentTextAttributeValueMarker: 'componentTextAttributeValueMarker', + componentTextAttributeValueData: 'componentTextAttributeValueData', + + // Text Span + textSpan: 'textSpan', + + // Others + formGfmTaskCheckbox: 'formGfmTaskCheckbox' + doubleBracket: 'doubleBracket' + } +} \ No newline at end of file diff --git a/src/micromark-extension/utils.ts b/src/micromark-extension/utils.ts index d4e5c3d..657e463 100644 --- a/src/micromark-extension/utils.ts +++ b/src/micromark-extension/utils.ts @@ -1,4 +1,4 @@ -import type { Effects, State, Code } from 'micromark-util-types' +import type { Effects, State, Code, TokenTypeMap } from './types' import { Codes } from './constants' // Measure the number of character codes in chunks. @@ -41,7 +41,7 @@ export function linePrefixSize (events: any[]) { /** * Manage token state */ -export const useTokenState = (tokenName: string) => { +export const useTokenState = (tokenName: keyof TokenTypeMap) => { const token = { isOpen: false, /** @@ -99,12 +99,12 @@ function checkCodeFenced (effects: Effects, ok: State, nok: State) { return start - function start (code: Code): State | void { + function start (code: Code): State | undefined { effects.enter('codeFenced') return after(code) } - function after (code: Code): State | void { + function after (code: Code): State | undefined { if (code === Codes.backTick) { backTickCount++ effects.consume(code)