-
Notifications
You must be signed in to change notification settings - Fork 5
/
mdx-text-expression.ts
37 lines (32 loc) · 1.26 KB
/
mdx-text-expression.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
* Copyright IBM Corp. 2022, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
import { MdxTextExpression } from 'mdast-util-mdx-expression'
import { ErrorNode } from '../error-node.js'
import { NoParentNodeException } from '../exceptions/no-parent-element-exception.js'
import { RestrictedSyntaxException } from '../exceptions/restricted-syntax-exception.js'
import { NodeHandler } from '../interfaces.js'
import { replaceNode } from '../replace-node.js'
/**
* Handles an mdxTextExpression node by removing it from the AST.
*
* Example: `Hello {'some text'}`
*
* @param data Incoming data for this node.
* @returns a VisitorResult.
*/
const mdxTextExpression: NodeHandler = (data, { onError }) => {
const nodeAsMdxTextExpression = data.node as Partial<MdxTextExpression>
if (!data.parent) {
throw new NoParentNodeException(`{${nodeAsMdxTextExpression.value}}`, data.node.position)
}
// mdxTextExpressions are not allowed, so store an error
const errorIndex = onError(
new RestrictedSyntaxException(`{${nodeAsMdxTextExpression.value}}`, data.node.position)
)
return replaceNode(data, new ErrorNode(errorIndex).serialize())
}
export { mdxTextExpression }