mirror of
https://github.com/explosion/spaCy.git
synced 2025-07-10 16:22:29 +03:00
Extract function to own file
This commit is contained in:
parent
e5c4b18ccc
commit
46fb76c281
38
website/plugins/getProps.mjs
Normal file
38
website/plugins/getProps.mjs
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
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 [left.name, right.value]
|
||||||
|
}
|
||||||
|
|
||||||
|
const getProps = (estree) => {
|
||||||
|
if (estree.type !== 'Program' || !estree.body || estree.body.length <= 0 || !estree.body[0]) {
|
||||||
|
return {}
|
||||||
|
}
|
||||||
|
|
||||||
|
const estreeBodyFirstNode = estree.body[0]
|
||||||
|
|
||||||
|
if (estreeBodyFirstNode.type !== 'ExpressionStatement' || !estreeBodyFirstNode.expression) {
|
||||||
|
return {}
|
||||||
|
}
|
||||||
|
|
||||||
|
const statement = estreeBodyFirstNode.expression
|
||||||
|
|
||||||
|
const attributeExpressions = [
|
||||||
|
...(statement.type === 'SequenceExpression' && statement.expressions
|
||||||
|
? statement.expressions
|
||||||
|
: []),
|
||||||
|
...(statement.type === 'AssignmentExpression' ? [statement] : []),
|
||||||
|
]
|
||||||
|
|
||||||
|
return Object.fromEntries(attributeExpressions.map(parseAttribute))
|
||||||
|
}
|
||||||
|
|
||||||
|
export default getProps
|
|
@ -1,16 +1,4 @@
|
||||||
const parseAttribute = (expression) => {
|
import getProps from './getProps.mjs'
|
||||||
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 handleNode = (node) => {
|
const handleNode = (node) => {
|
||||||
if (node.type === 'section' && node.children) {
|
if (node.type === 'section' && node.children) {
|
||||||
|
@ -31,34 +19,13 @@ const handleNode = (node) => {
|
||||||
return node
|
return node
|
||||||
}
|
}
|
||||||
|
|
||||||
const { estree } = lastNode.data
|
const data = node.data || (node.data = {})
|
||||||
|
data.hProperties = getProps(lastNode.data.estree)
|
||||||
|
|
||||||
|
// Only keep the text, drop the rest
|
||||||
|
node.children = [node.children[0]]
|
||||||
|
|
||||||
if (estree.type !== 'Program' || !estree.body || estree.body.length <= 0 || !estree.body[0]) {
|
|
||||||
return node
|
return node
|
||||||
}
|
|
||||||
|
|
||||||
const estreeBodyFirstNode = estree.body[0]
|
|
||||||
|
|
||||||
if (estreeBodyFirstNode.type !== 'ExpressionStatement' || !estreeBodyFirstNode.expression) {
|
|
||||||
return node
|
|
||||||
}
|
|
||||||
|
|
||||||
const statement = estreeBodyFirstNode.expression
|
|
||||||
|
|
||||||
const attributeExpressions = [
|
|
||||||
...(statement.type === 'SequenceExpression' && statement.expressions
|
|
||||||
? statement.expressions
|
|
||||||
: []),
|
|
||||||
...(statement.type === 'AssignmentExpression' ? [statement] : []),
|
|
||||||
]
|
|
||||||
|
|
||||||
// This replaces the markdown heading with a JSX element
|
|
||||||
return {
|
|
||||||
type: 'mdxJsxFlowElement',
|
|
||||||
name: `h${node.depth}`,
|
|
||||||
attributes: attributeExpressions.map(parseAttribute),
|
|
||||||
children: [node.children[0]],
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const parseAstTree = (markdownAST) => ({
|
const parseAstTree = (markdownAST) => ({
|
||||||
|
|
Loading…
Reference in New Issue
Block a user