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,43 @@
/**
* @this {TokenizeContext}
* @param {Effects} effects
* @param {State} ok
* @param {State} nok
* @param {Acorn | undefined} acorn
* @param {AcornOptions | undefined} acornOptions
* @param {boolean | undefined} addResult
* @param {boolean | undefined} allowLazy
* @param {TokenType} tagType
* @param {TokenType} tagMarkerType
* @param {TokenType} tagClosingMarkerType
* @param {TokenType} tagSelfClosingMarker
* @param {TokenType} tagNameType
* @param {TokenType} tagNamePrimaryType
* @param {TokenType} tagNameMemberMarkerType
* @param {TokenType} tagNameMemberType
* @param {TokenType} tagNamePrefixMarkerType
* @param {TokenType} tagNameLocalType
* @param {TokenType} tagExpressionAttributeType
* @param {TokenType} tagExpressionAttributeMarkerType
* @param {TokenType} tagExpressionAttributeValueType
* @param {TokenType} tagAttributeType
* @param {TokenType} tagAttributeNameType
* @param {TokenType} tagAttributeNamePrimaryType
* @param {TokenType} tagAttributeNamePrefixMarkerType
* @param {TokenType} tagAttributeNameLocalType
* @param {TokenType} tagAttributeInitializerMarkerType
* @param {TokenType} tagAttributeValueLiteralType
* @param {TokenType} tagAttributeValueLiteralMarkerType
* @param {TokenType} tagAttributeValueLiteralValueType
* @param {TokenType} tagAttributeValueExpressionType
* @param {TokenType} tagAttributeValueExpressionMarkerType
* @param {TokenType} tagAttributeValueExpressionValueType
*/
export function factoryTag(this: import("micromark-util-types").TokenizeContext, effects: Effects, ok: State, nok: State, acorn: Acorn | undefined, acornOptions: AcornOptions | undefined, addResult: boolean | undefined, allowLazy: boolean | undefined, tagType: TokenType, tagMarkerType: TokenType, tagClosingMarkerType: TokenType, tagSelfClosingMarker: TokenType, tagNameType: TokenType, tagNamePrimaryType: TokenType, tagNameMemberMarkerType: TokenType, tagNameMemberType: TokenType, tagNamePrefixMarkerType: TokenType, tagNameLocalType: TokenType, tagExpressionAttributeType: TokenType, tagExpressionAttributeMarkerType: TokenType, tagExpressionAttributeValueType: TokenType, tagAttributeType: TokenType, tagAttributeNameType: TokenType, tagAttributeNamePrimaryType: TokenType, tagAttributeNamePrefixMarkerType: TokenType, tagAttributeNameLocalType: TokenType, tagAttributeInitializerMarkerType: TokenType, tagAttributeValueLiteralType: TokenType, tagAttributeValueLiteralMarkerType: TokenType, tagAttributeValueLiteralValueType: TokenType, tagAttributeValueExpressionType: TokenType, tagAttributeValueExpressionMarkerType: TokenType, tagAttributeValueExpressionValueType: TokenType): (code: import("micromark-util-types").Code) => import("micromark-util-types").State | undefined;
export type Acorn = import('micromark-factory-mdx-expression').Acorn;
export type AcornOptions = import('micromark-factory-mdx-expression').AcornOptions;
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 TokenizeContext = import('micromark-util-types').TokenizeContext;
export type TokenType = import('micromark-util-types').TokenType;

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,30 @@
/**
* Parse JSX (flow).
*
* @param {Acorn | undefined} acorn
* Acorn parser to use (optional).
* @param {Options} options
* Configuration.
* @returns {Construct}
* Construct.
*/
export function jsxFlow(acorn: Acorn | undefined, options: Options): Construct;
export type Acorn = import('micromark-factory-mdx-expression').Acorn;
export type AcornOptions = import('micromark-factory-mdx-expression').AcornOptions;
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;
/**
* Configuration.
*/
export type Options = {
/**
* Acorn options.
*/
acornOptions: AcornOptions | undefined;
/**
* Whether to add `estree` fields to tokens with results from acorn.
*/
addResult: boolean | undefined;
};

View File

@@ -0,0 +1,169 @@
/**
* @typedef {import('micromark-factory-mdx-expression').Acorn} Acorn
* @typedef {import('micromark-factory-mdx-expression').AcornOptions} AcornOptions
* @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
*/
/**
* @typedef Options
* Configuration.
* @property {AcornOptions | undefined} acornOptions
* Acorn options.
* @property {boolean | undefined} addResult
* Whether to add `estree` fields to tokens with results from acorn.
*/
import {ok as assert} from 'devlop'
import {markdownLineEnding, markdownSpace} from 'micromark-util-character'
import {factorySpace} from 'micromark-factory-space'
import {codes, types} from 'micromark-util-symbol'
import {factoryTag} from './factory-tag.js'
/**
* Parse JSX (flow).
*
* @param {Acorn | undefined} acorn
* Acorn parser to use (optional).
* @param {Options} options
* Configuration.
* @returns {Construct}
* Construct.
*/
export function jsxFlow(acorn, options) {
return {name: 'mdxJsxFlowTag', tokenize: tokenizeJsxFlow, concrete: true}
/**
* MDX JSX (flow).
*
* ```markdown
* > | <A />
* ^^^^^
* ```
*
* @this {TokenizeContext}
* @type {Tokenizer}
*/
function tokenizeJsxFlow(effects, ok, nok) {
const self = this
return start
/**
* Start of MDX: JSX (flow).
*
* ```markdown
* > | <A />
* ^
* ```
*
* @type {State}
*/
function start(code) {
// To do: in `markdown-rs`, constructs need to parse the indent themselves.
// This should also be introduced in `micromark-js`.
assert(code === codes.lessThan, 'expected `<`')
return before(code)
}
/**
* After optional whitespace, before of MDX JSX (flow).
*
* ```markdown
* > | <A />
* ^
* ```
*
* @type {State}
*/
function before(code) {
return factoryTag.call(
self,
effects,
after,
nok,
acorn,
options.acornOptions,
options.addResult,
false,
'mdxJsxFlowTag',
'mdxJsxFlowTagMarker',
'mdxJsxFlowTagClosingMarker',
'mdxJsxFlowTagSelfClosingMarker',
'mdxJsxFlowTagName',
'mdxJsxFlowTagNamePrimary',
'mdxJsxFlowTagNameMemberMarker',
'mdxJsxFlowTagNameMember',
'mdxJsxFlowTagNamePrefixMarker',
'mdxJsxFlowTagNameLocal',
'mdxJsxFlowTagExpressionAttribute',
'mdxJsxFlowTagExpressionAttributeMarker',
'mdxJsxFlowTagExpressionAttributeValue',
'mdxJsxFlowTagAttribute',
'mdxJsxFlowTagAttributeName',
'mdxJsxFlowTagAttributeNamePrimary',
'mdxJsxFlowTagAttributeNamePrefixMarker',
'mdxJsxFlowTagAttributeNameLocal',
'mdxJsxFlowTagAttributeInitializerMarker',
'mdxJsxFlowTagAttributeValueLiteral',
'mdxJsxFlowTagAttributeValueLiteralMarker',
'mdxJsxFlowTagAttributeValueLiteralValue',
'mdxJsxFlowTagAttributeValueExpression',
'mdxJsxFlowTagAttributeValueExpressionMarker',
'mdxJsxFlowTagAttributeValueExpressionValue'
)(code)
}
/**
* After an MDX JSX (flow) tag.
*
* ```markdown
* > | <A>
* ^
* ```
*
* @type {State}
*/
function after(code) {
return markdownSpace(code)
? factorySpace(effects, end, types.whitespace)(code)
: end(code)
}
/**
* After an MDX JSX (flow) tag, after optional whitespace.
*
* ```markdown
* > | <A> <B>
* ^
* ```
*
* @type {State}
*/
function end(code) {
// We want to allow expressions directly after tags.
// See <https://github.com/micromark/micromark-extension-mdx-expression/blob/d5d92b9/packages/micromark-extension-mdx-expression/dev/lib/syntax.js#L183>
// for more info.
const leftBraceValue = self.parser.constructs.flow[codes.leftCurlyBrace]
/* c8 ignore next 5 -- always a list when normalized. */
const constructs = Array.isArray(leftBraceValue)
? leftBraceValue
: leftBraceValue
? [leftBraceValue]
: []
const expression = constructs.find((d) => d.name === 'mdxFlowExpression')
// Another tag.
return code === codes.lessThan
? // We cant just say: fine. Lines of blocks have to be parsed until an eol/eof.
start(code)
: code === codes.leftCurlyBrace && expression
? effects.attempt(expression, end, nok)(code)
: code === codes.eof || markdownLineEnding(code)
? ok(code)
: nok(code)
}
}
}

View File

@@ -0,0 +1,29 @@
/**
* Parse JSX (text).
*
* @param {Acorn | undefined} acorn
* Acorn parser to use (optional).
* @param {Options} options
* Configuration.
* @returns {Construct}
* Construct.
*/
export function jsxText(acorn: Acorn | undefined, options: Options): Construct;
export type Acorn = import('micromark-factory-mdx-expression').Acorn;
export type AcornOptions = import('micromark-factory-mdx-expression').AcornOptions;
export type Construct = import('micromark-util-types').Construct;
export type TokenizeContext = import('micromark-util-types').TokenizeContext;
export type Tokenizer = import('micromark-util-types').Tokenizer;
/**
* Configuration.
*/
export type Options = {
/**
* Acorn options.
*/
acornOptions: AcornOptions | undefined;
/**
* Whether to add `estree` fields to tokens with results from acorn.
*/
addResult: boolean | undefined;
};

View File

@@ -0,0 +1,81 @@
/**
* @typedef {import('micromark-factory-mdx-expression').Acorn} Acorn
* @typedef {import('micromark-factory-mdx-expression').AcornOptions} AcornOptions
* @typedef {import('micromark-util-types').Construct} Construct
* @typedef {import('micromark-util-types').TokenizeContext} TokenizeContext
* @typedef {import('micromark-util-types').Tokenizer} Tokenizer
*/
/**
* @typedef Options
* Configuration.
* @property {AcornOptions | undefined} acornOptions
* Acorn options.
* @property {boolean | undefined} addResult
* Whether to add `estree` fields to tokens with results from acorn.
*/
import {factoryTag} from './factory-tag.js'
/**
* Parse JSX (text).
*
* @param {Acorn | undefined} acorn
* Acorn parser to use (optional).
* @param {Options} options
* Configuration.
* @returns {Construct}
* Construct.
*/
export function jsxText(acorn, options) {
return {name: 'mdxJsxTextTag', tokenize: tokenizeJsxText}
/**
* MDX JSX (text).
*
* ```markdown
* > | a <b />.
* ^^^^^
* ```
*
* @this {TokenizeContext}
* @type {Tokenizer}
*/
function tokenizeJsxText(effects, ok, nok) {
return factoryTag.call(
this,
effects,
ok,
nok,
acorn,
options.acornOptions,
options.addResult,
true,
'mdxJsxTextTag',
'mdxJsxTextTagMarker',
'mdxJsxTextTagClosingMarker',
'mdxJsxTextTagSelfClosingMarker',
'mdxJsxTextTagName',
'mdxJsxTextTagNamePrimary',
'mdxJsxTextTagNameMemberMarker',
'mdxJsxTextTagNameMember',
'mdxJsxTextTagNamePrefixMarker',
'mdxJsxTextTagNameLocal',
'mdxJsxTextTagExpressionAttribute',
'mdxJsxTextTagExpressionAttributeMarker',
'mdxJsxTextTagExpressionAttributeValue',
'mdxJsxTextTagAttribute',
'mdxJsxTextTagAttributeName',
'mdxJsxTextTagAttributeNamePrimary',
'mdxJsxTextTagAttributeNamePrefixMarker',
'mdxJsxTextTagAttributeNameLocal',
'mdxJsxTextTagAttributeInitializerMarker',
'mdxJsxTextTagAttributeValueLiteral',
'mdxJsxTextTagAttributeValueLiteralMarker',
'mdxJsxTextTagAttributeValueLiteralValue',
'mdxJsxTextTagAttributeValueExpression',
'mdxJsxTextTagAttributeValueExpressionMarker',
'mdxJsxTextTagAttributeValueExpressionValue'
)
}
}

View File

@@ -0,0 +1,32 @@
/**
* Create an extension for `micromark` to enable MDX JSX syntax.
*
* @param {Options | null | undefined} [options]
* Configuration (optional).
* @returns {Extension}
* Extension for `micromark` that can be passed in `extensions` to enable MDX
* JSX syntax.
*/
export function mdxJsx(options?: Options | null | undefined): Extension;
export type Extension = import('micromark-util-types').Extension;
export type Acorn = import('micromark-factory-mdx-expression').Acorn;
export type AcornOptions = import('micromark-factory-mdx-expression').AcornOptions;
/**
* Configuration (optional).
*/
export type Options = {
/**
* Acorn parser to use (optional).
*/
acorn?: Acorn | null | undefined;
/**
* Configuration for acorn (default: `{ecmaVersion: 2024, locations: true,
* sourceType: 'module'}`); all fields except `locations` can be set.
*/
acornOptions?: AcornOptions | null | undefined;
/**
* Whether to add `estree` fields to tokens with results from acorn
* (default: `false`).
*/
addResult?: boolean | null | undefined;
};

View File

@@ -0,0 +1,69 @@
/**
* @typedef {import('micromark-util-types').Extension} Extension
* @typedef {import('micromark-factory-mdx-expression').Acorn} Acorn
* @typedef {import('micromark-factory-mdx-expression').AcornOptions} AcornOptions
*/
/**
* @typedef Options
* Configuration (optional).
* @property {Acorn | null | undefined} [acorn]
* Acorn parser to use (optional).
* @property {AcornOptions | null | undefined} [acornOptions]
* Configuration for acorn (default: `{ecmaVersion: 2024, locations: true,
* sourceType: 'module'}`); all fields except `locations` can be set.
* @property {boolean | null | undefined} [addResult=false]
* Whether to add `estree` fields to tokens with results from acorn
* (default: `false`).
*/
import {codes} from 'micromark-util-symbol'
import {jsxText} from './jsx-text.js'
import {jsxFlow} from './jsx-flow.js'
/**
* Create an extension for `micromark` to enable MDX JSX syntax.
*
* @param {Options | null | undefined} [options]
* Configuration (optional).
* @returns {Extension}
* Extension for `micromark` that can be passed in `extensions` to enable MDX
* JSX syntax.
*/
export function mdxJsx(options) {
const settings = options || {}
const acorn = settings.acorn
/** @type {AcornOptions | undefined} */
let acornOptions
if (acorn) {
if (!acorn.parse || !acorn.parseExpressionAt) {
throw new Error(
'Expected a proper `acorn` instance passed in as `options.acorn`'
)
}
acornOptions = Object.assign(
{ecmaVersion: 2024, sourceType: 'module'},
settings.acornOptions,
{locations: true}
)
} else if (settings.acornOptions || settings.addResult) {
throw new Error('Expected an `acorn` instance passed in as `options.acorn`')
}
return {
flow: {
[codes.lessThan]: jsxFlow(acorn || undefined, {
acornOptions,
addResult: settings.addResult || undefined
})
},
text: {
[codes.lessThan]: jsxText(acorn || undefined, {
acornOptions,
addResult: settings.addResult || undefined
})
}
}
}