Skip to content

Commit

Permalink
fixup! feat(json): add legacy JSON generator
Browse files Browse the repository at this point in the history
  • Loading branch information
RedYetiDev committed Nov 10, 2024
1 parent 35a89cf commit 6e80d01
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
11 changes: 9 additions & 2 deletions src/queries.mjs
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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];
Expand Down
1 change: 1 addition & 0 deletions src/test/metadata.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
33 changes: 22 additions & 11 deletions src/test/queries.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 [`<string>`](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', () => {
Expand Down

0 comments on commit 6e80d01

Please sign in to comment.