Skip to content

Commit

Permalink
Adds support for dropping location information in parse function (#455
Browse files Browse the repository at this point in the history
)

* Adds support for dropping location information in `parse` function

* Prettier
  • Loading branch information
rpaul-stripe authored Oct 12, 2023
1 parent 1d4515a commit 0d078ed
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@markdoc/markdoc",
"author": "Ryan Paul",
"version": "0.3.2",
"version": "0.3.3",
"description": "A text markup language for documentation",
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down
20 changes: 20 additions & 0 deletions src/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,26 @@ describe('Markdown parser', function () {
},
});
});

it('location off', function () {
const example = convert(`# This is a test`, {
file: 'foo.md',
location: false,
});
expect(example.children[0].location).toBeUndefined();
});

it('location on', function () {
const example = convert(`# This is a test`, {
file: 'foo.md',
location: true,
});
const expected = { file: 'foo.md', start: { line: 0 }, end: { line: 1 } };
expect(example.children[0].location).toDeepEqualSubset(expected);

const example2 = convert(`# This is a test`, { file: 'foo.md' });
expect(example2.children[0].location).toDeepEqualSubset(expected);
});
});

describe('handling frontmatter', function () {
Expand Down
31 changes: 17 additions & 14 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ function handleToken(
nodes: Node[],
file?: string,
handleSlots?: boolean,
addLocation?: boolean,
inlineParent?: Node
) {
if (token.type === 'frontmatter') {
Expand Down Expand Up @@ -150,18 +151,20 @@ function handleToken(
const { position = {} } = token;

node.errors = errors;
node.lines = token.map || parent.lines || [];
node.location = {
file,
start: {
line: node.lines[0],
character: position.start,
},
end: {
line: node.lines[1],
character: position.end,
},
};
if (addLocation !== false) {
node.lines = token.map || parent.lines || [];
node.location = {
file,
start: {
line: node.lines[0],
character: position.start,
},
end: {
line: node.lines[1],
character: position.end,
},
};
}

if (inlineParent) node.inline = true;

Expand All @@ -187,7 +190,7 @@ function handleToken(
const isLeafNode = typeName === 'image';
if (!isLeafNode) {
for (const child of token.children)
handleToken(child, nodes, file, handleSlots, inlineParent);
handleToken(child, nodes, file, handleSlots, addLocation, inlineParent);
}

nodes.pop();
Expand All @@ -200,7 +203,7 @@ export default function parser(tokens: Token[], args?: string | ParserArgs) {
if (typeof args === 'string') args = { file: args };

for (const token of tokens)
handleToken(token, nodes, args?.file, args?.slots);
handleToken(token, nodes, args?.file, args?.slots, args?.location);

if (nodes.length > 1)
for (const node of nodes.slice(1))
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export type ConfigType = Partial<{
validation?: {
parents?: Node[];
validateFunctions?: boolean;
environment?: string;
};
}>;

Expand Down Expand Up @@ -168,4 +169,5 @@ export type Value = AstType | Scalar;
export type ParserArgs = {
file?: string;
slots?: boolean;
location?: boolean;
};

0 comments on commit 0d078ed

Please sign in to comment.