diff --git a/src/queries.mjs b/src/queries.mjs index 55022d7..74185ef 100644 --- a/src/queries.mjs +++ b/src/queries.mjs @@ -1,7 +1,7 @@ 'use strict'; import { u as createTree } from 'unist-builder'; -import { SKIP } from 'unist-util-visit'; +import { SKIP, visit } from 'unist-util-visit'; import { DOC_API_STABILITY_SECTION_REF_URL } from './constants.mjs'; @@ -81,8 +81,15 @@ const createQueries = () => { transformTypeToReferenceLink ); - const newNode = remark.parse(replacedTypes); + const { + children: [newNode], + } = remark.parse(replacedTypes); const index = parent.children.indexOf(node); + const originalPosition = node.position; + visit(newNode, node => { + (node.position.start += originalPosition.start), + (node.position.end += originalPosition.end); + }); parent.children.splice(index, 1, ...newNode.children); return [SKIP]; diff --git a/src/test/metadata.test.mjs b/src/test/metadata.test.mjs index 9c9e25d..b89d82b 100644 --- a/src/test/metadata.test.mjs +++ b/src/test/metadata.test.mjs @@ -72,6 +72,7 @@ describe('createMetadata', () => { deprecated_in: undefined, heading, n_api_version: undefined, + rawContent: '', removed_in: undefined, slug: 'test-heading', source_link: 'test.com', diff --git a/src/test/queries.test.mjs b/src/test/queries.test.mjs index 35d93ac..1fbab4b 100644 --- a/src/test/queries.test.mjs +++ b/src/test/queries.test.mjs @@ -15,23 +15,34 @@ describe('createQueries', () => { }); // valid type - it('should update type to reference correctly', () => { + it.only('should update type to reference correctly', () => { const queries = createQueries(); - const node = { value: 'This is a {string} type.' }; - queries.updateTypeReference(node); - strictEqual(node.type, 'html'); - strictEqual( - node.value, - 'This is a [``](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) type.' + const node = { + value: 'This is a {string} type.', + position: { start: 0, end: 0 }, + }; + const parent = { children: [node] }; + queries.updateTypeReference(node, parent); + deepStrictEqual( + parent.children.map(c => c.value), + [ + 'This is a ', + undefined, // link + ' type.', + ] ); }); it('should update type to reference not correctly if no match', () => { const queries = createQueries(); - const node = { value: 'This is a {test} type.' }; - queries.updateTypeReference(node); - strictEqual(node.type, 'html'); - strictEqual(node.value, 'This is a {test} type.'); + const node = { + value: 'This is a {test} type.', + position: { start: 0, end: 0 }, + }; + const parent = { children: [node] }; + queries.updateTypeReference(node, parent); + strictEqual(parent.children[0].type, 'text'); + strictEqual(parent.children[0].value, 'This is a {test} type.'); }); it('should add heading metadata correctly', () => {