Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly sort YAML map keys with mixed types #441

Merged
merged 6 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## 3.5.1

Fixes:

- Correctly sort YAML map keys with mixed types. (#433, #441)

## 3.5.0 (2024-04-29)

Development:
Expand Down
31 changes: 28 additions & 3 deletions markdown.dtx
Original file line number Diff line number Diff line change
Expand Up @@ -32359,13 +32359,38 @@ M.extensions.jekyll_data = function(expect_jekyll_data)
for k, _ in pairs(d) do
table.insert(keys, k)
end
table.sort(keys)
% \end{macrocode}
% \begin{markdown}
%
% For reproducibility, sort the keys. For mixed string-and-numeric keys, sort
% numeric keys before string keys.
%
% \end{markdown}
% \begin{macrocode}
table.sort(keys, function(first, second)
if type(first) ~= type(second) then
return type(first) < type(second)
else
return first < second
end
end)

if not p then
table.insert(buf, "\\markdownRendererJekyllDataBegin")
end

if #d > 0 then
local is_sequence = false
if #d > 0 and #d == #keys then
for i=1, #d do
if d[i] == nil then
goto not_a_sequence
end
end
is_sequence = true
end
::not_a_sequence::

if is_sequence then
table.insert(buf, "\\markdownRendererJekyllDataSequenceBegin{")
table.insert(buf, self.identifier(p or "null"))
table.insert(buf, "}{")
Expand Down Expand Up @@ -32420,7 +32445,7 @@ M.extensions.jekyll_data = function(expect_jekyll_data)
end
end

if #d > 0 then
if is_sequence then
table.insert(buf, "\\markdownRendererJekyllDataSequenceEnd")
else
table.insert(buf, "\\markdownRendererJekyllDataMappingEnd")
Expand Down
32 changes: 32 additions & 0 deletions tests/testfiles/lunamark-markdown/jekyll-data-mixed-keys.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
\markdownSetup{jekyllData=true}
<<<
This test ensures that the Lua `jekyllData` option correctly propagates
through the plain TeX interface and that mixed numeric and string keys
are correctly sorted.

---
1: foo
a: bar
---
>>>
BEGIN document
codeSpan: jekyllData
softLineBreak
softLineBreak
interblockSeparator
jekyllDataBegin
BEGIN jekyllDataMappingBegin
- key: null
- length: 2
END jekyllDataMappingBegin
BEGIN jekyllDataString
- key: 1
- value: foo
END jekyllDataString
BEGIN jekyllDataString
- key: a
- value: bar
END jekyllDataString
jekyllDataMappingEnd
jekyllDataEnd
END document
Loading