Skip to content

Commit

Permalink
fix: parsing yaml line (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmjuanes authored Aug 3, 2024
1 parent c389630 commit eefd8e7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ const yamlParser = (str = "") => {
level = level - 1;
}
const isArrayItem = line.trim().startsWith("-");
let [key, value] = line.trim().split(":").map(v => v.trim());
// TODO: improve the regex to capture the line structure
let [_, key, value] = (line.trim().match(/^([^:"]*):(.*)$/m) || ["", line]).map(v => v.trim());
// Check if is a new item of the array
if (isArrayItem) {
key = key.replace(/^(- *)/m, "");
Expand Down
14 changes: 14 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,20 @@ describe("utils", () => {
assert.equal(json.items[2].items[1], "bar");
assert.equal(json.foo, "bar");
});

it("should support ':' characters in value", () => {
const json = yaml([
`link1: "https://www.example1.com"`,
`link2: "https://www.example2.com"`,
`items:`,
` - "https://www.example3.com"`,
` - key: "https://www.example4.com"`,
]);
assert.equal(json.link1, "https://www.example1.com");
assert.equal(json.link2, "https://www.example2.com");
assert.equal(json.items[0], "https://www.example3.com");
assert.equal(json.items[1].key, "https://www.example4.com");
});
});

describe("frontmatter", () => {
Expand Down

0 comments on commit eefd8e7

Please sign in to comment.