Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add positional info to attributes #11

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
41 changes: 35 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* @typedef {import('../index.js').MdxJsxExpressionAttribute} MdxJsxExpressionAttribute
* @typedef {import('../index.js').MdxJsxFlowElement} MdxJsxFlowElement
* @typedef {import('../index.js').MdxJsxTextElement} MdxJsxTextElement
*
* @typedef {import('unist').Point} Point
*/

/**
Expand Down Expand Up @@ -136,6 +138,18 @@ export function mdxJsxFromMarkdown() {
this.buffer()
}

/**
* Copy a point-like value.
*
* @param {Point} d
* Point-like value.
* @returns {Point}
* unist point.
*/
function point(d) {
return {line: d.line, column: d.column, offset: d.offset}
}

/**
* @this {CompileContext}
* @type {FromMarkdownHandle}
Expand Down Expand Up @@ -263,7 +277,16 @@ export function mdxJsxFromMarkdown() {
const tag = this.data.mdxJsxTag
assert(tag, 'expected `mdxJsxTag`')
enterMdxJsxTagAnyAttribute.call(this, token)
tag.attributes.push({type: 'mdxJsxAttribute', name: '', value: null})
tag.attributes.push({
type: 'mdxJsxAttribute',
name: '',
value: null,
position: {
start: point(token.start),
// @ts-expect-error: `end` will be patched later.
end: undefined
}
})
}

/**
Expand Down Expand Up @@ -306,6 +329,8 @@ export function mdxJsxFromMarkdown() {
const node = tag.attributes[tag.attributes.length - 1]
assert(node.type === 'mdxJsxAttribute')
node.name = this.sliceSerialize(token)
assert(node.position !== undefined)
node.position.end = point(token.end)
}

/**
Expand All @@ -318,19 +343,21 @@ export function mdxJsxFromMarkdown() {
const node = tag.attributes[tag.attributes.length - 1]
assert(node.type === 'mdxJsxAttribute')
node.name += ':' + this.sliceSerialize(token)
assert(node.position !== undefined)
node.position.end = point(token.end)
}

/**
* @this {CompileContext}
* @type {FromMarkdownHandle}
*/
function exitMdxJsxTagAttributeValueLiteral() {
function exitMdxJsxTagAttributeValueLiteral(token) {
const tag = this.data.mdxJsxTag
assert(tag, 'expected `mdxJsxTag`')
tag.attributes[tag.attributes.length - 1].value = parseEntities(
this.resume(),
{nonTerminated: false}
)
const node = tag.attributes[tag.attributes.length - 1]
node.value = parseEntities(this.resume(), {nonTerminated: false})
assert(node.position !== undefined)
node.position.end = point(token.end)
}

/**
Expand All @@ -351,6 +378,8 @@ export function mdxJsxFromMarkdown() {
}

tail.value = node
assert(tail.position !== undefined)
tail.position.end = point(token.end)
}

/**
Expand Down
Loading
Loading