-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement getJsonPathForNode (#13)
* feat: introduce getJsonPathForNode * revert: yarn.lock * refactor: remove "!" clobbers * chore: bump deps * chore: remove no longer required types def * feat: update types
- Loading branch information
Showing
9 changed files
with
181 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import * as fs from 'fs'; | ||
import { join } from 'path'; | ||
import { IParagraph, IStrong } from '../ast-types/mdast'; | ||
import { getJsonPathForNode } from '../getJsonPathForNode'; | ||
import { parseWithPointers } from '../parseWithPointers'; | ||
|
||
const FIXTURES_DIR = join(__dirname, '../reader/__tests__/fixtures/markdown/'); | ||
|
||
const basic = fs.readFileSync(join(FIXTURES_DIR, 'basic.md'), 'utf-8'); | ||
|
||
describe('getJsonPathForNode', () => { | ||
describe('basic fixture', () => { | ||
const result = parseWithPointers(basic); | ||
|
||
it('generates correct json path for heading', () => { | ||
expect(getJsonPathForNode(result.data, result.data.children[0])).toEqual(['children', 0]); | ||
}); | ||
|
||
it('generates correct json path for strong node in paragraph', () => { | ||
expect(getJsonPathForNode(result.data, (result.data.children[1] as IParagraph).children[3])).toEqual([ | ||
'children', | ||
1, | ||
'children', | ||
3, | ||
]); | ||
}); | ||
|
||
it('generates correct json path for nested emphasis node', () => { | ||
expect( | ||
getJsonPathForNode(result.data, ((result.data.children[1] as IParagraph).children[7] as IStrong).children[1]), | ||
).toEqual(['children', 1, 'children', 7, 'children', 1]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { JsonPath } from '@stoplight/types'; | ||
import * as Unist from 'unist'; | ||
|
||
export const getJsonPathForNode = (root: Unist.Parent, node: Unist.Node): JsonPath | void => { | ||
const path: JsonPath = []; | ||
findNode(root, node, path); | ||
return path; | ||
}; | ||
|
||
function findNode(root: Unist.Parent | Unist.Node, node: Unist.Node, path: JsonPath): Unist.Node | undefined { | ||
if (node.position === undefined || root.position === undefined) return; | ||
|
||
if ( | ||
node.position.start.line === root.position.start.line && | ||
node.position.end.line === root.position.end.line && | ||
node.position.start.column === root.position.start.column && | ||
node.position.end.column === root.position.end.column | ||
) { | ||
return node; | ||
} | ||
|
||
if (node.position.start.line >= root.position.start.line && node.position.end.line <= root.position.end.line) { | ||
const { children } = root; | ||
if (Array.isArray(children)) { | ||
for (let i = 0; i < children.length; i++) { | ||
const item = findNode(children[i], node, path); | ||
if (item) { | ||
path.unshift('children', i); | ||
return findNode(item, node, path); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.