mirror of
https://github.com/explosion/spaCy.git
synced 2025-08-09 22:54:53 +03:00
Allow custom attributes to parse single attributes as well
This commit is contained in:
parent
cc1597e4c5
commit
67893b4d90
|
@ -1,3 +1,17 @@
|
||||||
|
const parseAttribute = (expression) => {
|
||||||
|
if (expression.type !== 'AssignmentExpression' || !expression.left || !expression.right) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const { left, right } = expression
|
||||||
|
|
||||||
|
if (left.type !== 'Identifier' || right.type !== 'Literal' || !left.name || !right.value) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return { type: 'mdxJsxAttribute', name: left.name, value: right.value }
|
||||||
|
}
|
||||||
|
|
||||||
const parseAstTree = (markdownAST) => {
|
const parseAstTree = (markdownAST) => {
|
||||||
markdownAST.children.map((node, index) => {
|
markdownAST.children.map((node, index) => {
|
||||||
if (node.type !== 'heading' || !node.children || node.children < 2) {
|
if (node.type !== 'heading' || !node.children || node.children < 2) {
|
||||||
|
@ -24,48 +38,24 @@ const parseAstTree = (markdownAST) => {
|
||||||
|
|
||||||
const estreeBodyFirstNode = estree.body[0]
|
const estreeBodyFirstNode = estree.body[0]
|
||||||
|
|
||||||
if (
|
if (estreeBodyFirstNode.type !== 'ExpressionStatement' || !estreeBodyFirstNode.expression) {
|
||||||
estreeBodyFirstNode.type !== 'ExpressionStatement' ||
|
|
||||||
!estreeBodyFirstNode.expression ||
|
|
||||||
estreeBodyFirstNode.expression.type !== 'SequenceExpression'
|
|
||||||
) {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const sequenceExpression = estreeBodyFirstNode.expression
|
const statement = estreeBodyFirstNode.expression
|
||||||
|
|
||||||
if (!sequenceExpression.expressions) {
|
const attributeExpressions = [
|
||||||
return
|
...(statement.type === 'SequenceExpression' && statement.expressions
|
||||||
}
|
? statement.expressions
|
||||||
|
: []),
|
||||||
const attributes = sequenceExpression.expressions.map((expression) => {
|
...(statement.type === 'AssignmentExpression' ? [statement] : []),
|
||||||
if (
|
]
|
||||||
expression.type !== 'AssignmentExpression' ||
|
|
||||||
!expression.left ||
|
|
||||||
!expression.right
|
|
||||||
) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const { left, right } = expression
|
|
||||||
|
|
||||||
if (
|
|
||||||
left.type !== 'Identifier' ||
|
|
||||||
right.type !== 'Literal' ||
|
|
||||||
!left.name ||
|
|
||||||
!right.value
|
|
||||||
) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
return { type: 'mdxJsxAttribute', name: left.name, value: right.value }
|
|
||||||
})
|
|
||||||
|
|
||||||
// This replaces the markdown heading with a JSX element
|
// This replaces the markdown heading with a JSX element
|
||||||
markdownAST.children[index] = {
|
markdownAST.children[index] = {
|
||||||
type: 'mdxJsxFlowElement',
|
type: 'mdxJsxFlowElement',
|
||||||
name: `h${node.depth}`,
|
name: `h${node.depth}`,
|
||||||
attributes,
|
attributes: attributeExpressions.map(parseAttribute),
|
||||||
children: [node.children[0]],
|
children: [node.children[0]],
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue
Block a user