Skip to content

Commit

Permalink
fix whitespace not respected in snippets
Browse files Browse the repository at this point in the history
Fixes #853
  • Loading branch information
mikebarkmin committed Mar 22, 2024
1 parent 19666ab commit 5792288
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/flat-frogs-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hyperbook/fs": patch
---

fix snippets not respecting whitespace
2 changes: 1 addition & 1 deletion packages/fs/src/vfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ export const getMarkdown = async (
);
const template = handlebars.compile(snippetFile);
const vars = {};
for (const m of snippet[4].split(" ")) {
for (const m of snippet[4].match(/(?:[^\s"]+|"[^"]*")+/g) || []) {
const r = varReg.exec(m);
if (r) {
if (r[2].startsWith('"') && r[2].endsWith('"')) {
Expand Down
33 changes: 8 additions & 25 deletions packages/fs/tests/__snapshots__/hyperbook.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,9 @@ exports[`hyperbook > should get navigation 1`] = `
"repo": "https://github.com/openpatch/hyperbook/edit/main/website/en/book/paradigms.md",
},
"next": {
"href": "/subsection3",
"isEmpty": false,
"name": "Use Template1 with YAML",
"pages": [
{
"href": "/subsection3/single-template",
"name": "single-template",
"repo": "https://github.com/openpatch/hyperbook/edit/main/website/en/book/subsection3/single-template.md.hbs",
},
],
"repo": "https://github.com/openpatch/hyperbook/edit/main/website/en/book/subsection3/index.yml",
"sections": [
{
"isEmpty": true,
"name": "subsection31",
"pages": [
{
"href": "/subsection3/subsection31/test",
"name": "Test",
"repo": "https://github.com/openpatch/hyperbook/edit/main/website/en/book/subsection3/subsection31/test.md",
},
],
"sections": [],
},
],
"href": "/whitespace-test",
"name": "whitespace-test",
"repo": "https://github.com/openpatch/hyperbook/edit/main/website/en/book/whitespace-test.md",
},
"pages": [
{
Expand All @@ -45,6 +23,11 @@ exports[`hyperbook > should get navigation 1`] = `
"name": "Paradigms",
"repo": "https://github.com/openpatch/hyperbook/edit/main/website/en/book/paradigms.md",
},
{
"href": "/whitespace-test",
"name": "whitespace-test",
"repo": "https://github.com/openpatch/hyperbook/edit/main/website/en/book/whitespace-test.md",
},
],
"previous": {
"href": "/",
Expand Down
71 changes: 71 additions & 0 deletions packages/fs/tests/__snapshots__/vfile.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ Example use of a template.
}
`;

exports[`getMarkdown > should render snippet with whitespace 1`] = `
{
"content": "\\"My Title\\"
This is a test description!
",
"data": {},
}
`;

exports[`list > should get references 1`] = `
[
{
Expand Down Expand Up @@ -202,6 +213,26 @@ exports[`list > should list all 1`] = `
},
"root": "single-hyperbook",
},
{
"extension": ".md",
"folder": "book",
"markdown": {
"content": "\\"My Title\\"
This is a test description!
",
"data": {},
},
"name": "whitespace-test",
"path": {
"absolute": "single-hyperbook/book/whitespace-test.md",
"directory": "",
"href": "/whitespace-test",
"relative": "whitespace-test.md",
},
"root": "single-hyperbook",
},
{
"extension": ".md",
"folder": "book",
Expand Down Expand Up @@ -409,6 +440,26 @@ Example use of a template.
},
"root": "single-hyperbook",
},
{
"extension": ".md",
"folder": "book",
"markdown": {
"content": "\\"My Title\\"
This is a test description!
",
"data": {},
},
"name": "whitespace-test",
"path": {
"absolute": "single-hyperbook/book/whitespace-test.md",
"directory": "",
"href": "/whitespace-test",
"relative": "whitespace-test.md",
},
"root": "single-hyperbook",
},
{
"extension": ".md",
"folder": "book",
Expand Down Expand Up @@ -621,6 +672,26 @@ exports[`list > should list for folder book 1`] = `
},
"root": "single-hyperbook",
},
{
"extension": ".md",
"folder": "book",
"markdown": {
"content": "\\"My Title\\"
This is a test description!
",
"data": {},
},
"name": "whitespace-test",
"path": {
"absolute": "single-hyperbook/book/whitespace-test.md",
"directory": "",
"href": "/whitespace-test",
"relative": "whitespace-test.md",
},
"root": "single-hyperbook",
},
{
"extension": ".md",
"folder": "book",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:snippet{#whitespace title="My Title" description="This is a test description!"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"{{{title}}}"

{{{description}}}
11 changes: 11 additions & 0 deletions packages/fs/tests/vfile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ describe("getMarkdown", () => {
expect(await vfile.getMarkdown(templateFile)).toMatchSnapshot();
});

it("should render snippet with whitespace", async () => {
let files = await vfile.list(hyperbookPath);
let whitespaceTest = files.find(
(f) => f.path.relative === "whitespace-test.md" && f.folder === "book"
);
if (!whitespaceTest) {
throw Error("File not found");
}
expect(await vfile.getMarkdown(whitespaceTest)).toMatchSnapshot();
});

it("should get markdown from template for index", async () => {
let files = await vfile.list(hyperbookPath);
let templateFile = files.find(
Expand Down

0 comments on commit 5792288

Please sign in to comment.