Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions packages/language-service/lib/virtual-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,48 @@ function padOffsets(mapping, padding) {
}
}

/** @type {Partial<Record<Nodes["type"], string>>} */
const mdastElementNodeNameMap = {
blockquote: 'blockquote',
break: 'br',
delete: 'del',
emphasis: 'em',
html: 'html',
image: 'img',
imageReference: 'img',
inlineCode: 'code',
link: 'a',
linkReference: 'a',
listItem: 'li',
paragraph: 'p',
strong: 'strong',
table: 'table',
tableCell: 'td',
tableRow: 'tr',
thematicBreak: 'hr'
}
/**
* Get the JSX node names for a given AST node.
* @param {Nodes} node
* @returns {string[]}
*/
function getJsxNodeNameForMdast(node) {
if (node.type === 'code') {
return ['pre', 'code']
}
if (node.type === 'heading') {
return ['h' + node.depth]
}
if (node.type === 'list') {
return node.ordered ? ['ol'] : ['ul']
}
if (mdastElementNodeNameMap[node.type]) {
return [/** @type string */ (mdastElementNodeNameMap[node.type])]
}
// JSX Fragment
return ['']
}

/**
* @param {string} mdx
* @param {Root} ast
Expand Down Expand Up @@ -739,7 +781,7 @@ function getEmbeddedCodes(
}

default: {
jsx += jsxIndent + '<>'
jsx += jsxIndent + getJsxNodeNameForMdast(node).map((name) => `<${name}>`).join('')
break
}
}
Expand Down Expand Up @@ -789,7 +831,7 @@ function getEmbeddedCodes(
}

default: {
jsx += jsxIndent + '</>'
jsx += jsxIndent + getJsxNodeNameForMdast(node).map((name) => `</${name}>`).join('')
Comment thread
guyutongxue marked this conversation as resolved.
Outdated
break
}
}
Expand Down
Loading