Skip to content

Commit 25ce4a6

Browse files
committed
Add quotes by default to yaml dumps
1 parent f08be13 commit 25ce4a6

File tree

6 files changed

+21
-29
lines changed

6 files changed

+21
-29
lines changed

lua/obsidian/yaml.lua

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,14 @@ local yaml = {}
66
---Deserialize a YAML string.
77
yaml.loads = _yaml.eval
88

9-
local should_quote = function(s)
10-
local found = string.find(s, [[']], 1, true)
11-
return found ~= nil
12-
end
13-
149
---@return string[]
1510
local dumps
1611
dumps = function(x, indent, order)
1712
local indent_str = string.rep(" ", indent)
1813

1914
if type(x) == "string" then
20-
if should_quote(x) then
21-
return { indent_str .. [["]] .. x .. [["]] }
22-
else
23-
return { indent_str .. x }
24-
end
15+
-- TODO: handle double quotes in x
16+
return { indent_str .. [["]] .. x .. [["]] }
2517
end
2618

2719
if type(x) == "boolean" then
@@ -82,7 +74,7 @@ end
8274

8375
---Dump an object to a YAML string.
8476
---@param x any
85-
---@param order function
77+
---@param order function|?
8678
---@return string
8779
yaml.dumps = function(x, order)
8880
return table.concat(dumps(x, 0, order), "\n")

test/obsidian/note_spec.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ describe("Note", function()
7070
table.concat(note:frontmatter_lines(), "\n"),
7171
table.concat({
7272
"---",
73-
"id: note_with_additional_metadata",
73+
'id: "note_with_additional_metadata"',
7474
"aliases:",
75-
" - Note with additional metadata",
75+
' - "Note with additional metadata"',
7676
"tags: []",
77-
"foo: bar",
77+
'foo: "bar"',
7878
"---",
7979
}, "\n")
8080
)

test/obsidian/yaml_spec.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ describe("obsidian.yaml", function()
55
assert.equals(yaml.dumps(1), "1")
66
end)
77
it("should dump strings", function()
8-
assert.equals(yaml.dumps "hi there", "hi there")
8+
assert.equals(yaml.dumps "hi there", '"hi there"')
99
assert.equals(yaml.dumps "hi it's me", [["hi it's me"]])
10-
assert.equals(yaml.dumps { foo = "bar" }, [[foo: bar]])
10+
assert.equals(yaml.dumps { foo = "bar" }, [[foo: "bar"]])
1111
end)
1212
it("should dump strings with single quote", function()
1313
assert.equals(yaml.dumps "hi it's me", [["hi it's me"]])
1414
end)
1515
it("should dump table with string values", function()
16-
assert.equals(yaml.dumps { foo = "bar" }, [[foo: bar]])
16+
assert.equals(yaml.dumps { foo = "bar" }, [[foo: "bar"]])
1717
end)
1818
it("should dump arrays with string values", function()
19-
assert.equals(yaml.dumps { "foo", "bar" }, "- foo\n- bar")
19+
assert.equals(yaml.dumps { "foo", "bar" }, '- "foo"\n- "bar"')
2020
end)
2121
it("should dump arrays with number values", function()
2222
assert.equals(yaml.dumps { 1, 2 }, "- 1\n- 2")
@@ -25,13 +25,13 @@ describe("obsidian.yaml", function()
2525
assert.equals(yaml.dumps { { a = 1 }, { b = 2 } }, "- a: 1\n- b: 2")
2626
end)
2727
it("should dump tables with string values", function()
28-
assert.equals(yaml.dumps { a = "foo", b = "bar" }, "a: foo\nb: bar")
28+
assert.equals(yaml.dumps { a = "foo", b = "bar" }, 'a: "foo"\nb: "bar"')
2929
end)
3030
it("should dump tables with number values", function()
3131
assert.equals(yaml.dumps { a = 1, b = 2 }, "a: 1\nb: 2")
3232
end)
3333
it("should dump tables with array values", function()
34-
assert.equals(yaml.dumps { a = { "foo" }, b = { "bar" } }, "a:\n - foo\nb:\n - bar")
34+
assert.equals(yaml.dumps { a = { "foo" }, b = { "bar" } }, 'a:\n - "foo"\nb:\n - "bar"')
3535
end)
3636
it("should dump tables with empty array", function()
3737
assert.equals(yaml.dumps { a = {} }, "a: []")

test_fixtures/notes/foo_bar.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
---
2-
id: foo
2+
id: "foo"
33
aliases:
4-
- foo
5-
- Foo
6-
- Foo Bar
4+
- "foo"
5+
- "Foo"
6+
- "Foo Bar"
77
tags: []
88
---
99

test_fixtures/notes/note_with_additional_metadata_saved.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
---
2-
id: note_with_additional_metadata
2+
id: "note_with_additional_metadata"
33
aliases:
4-
- Note with additional metadata
4+
- "Note with additional metadata"
55
tags: []
6-
foo: bar
6+
foo: "bar"
77
---
88

99
# Note with additional metadata

test_fixtures/notes/note_without_frontmatter_saved.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
2-
id: test_fixtures/notes/note_without_frontmatter.md
2+
id: "test_fixtures/notes/note_without_frontmatter.md"
33
aliases:
4-
- Hey there
4+
- "Hey there"
55
tags: []
66
---
77

0 commit comments

Comments
 (0)