diff options
authorBryce Kwon <bryce@brycekwon.com>
committerBryce Kwon <bryce@brycekwon.com>
commit
parent
tree
download
Let a table cut a paragraph short
A table written right under a line of text came out as more of the paragraph, pipes and all.
-rw-r--r--custom/extensions/about-render.lua17
-rw-r--r--tests/extensions/test-about-render.lua3
2 files changed, 18 insertions, 2 deletions
diff --git a/custom/extensions/about-render.lua b/custom/extensions/about-render.lua
index af18d1b..ed7e340 100644
--- a/custom/extensions/about-render.lua
+++ b/custom/extensions/about-render.lua
@@ -377,6 +377,19 @@ if has_lpeg then
local function ordered(mark) return mark:match("%d") ~= nil end
+ local function table_starts(lines, i)
+ return i < #lines and lines[i]:find("|", 1, true) ~= nil
+ and table_delimiter:match(lines[i + 1]) ~= nil
+ end
+
+ -- A blank line ends a paragraph, and so does the start of any other
+ -- block. That includes a table, which GitHub's own parser lets cut a
+ -- paragraph short.
+ local function ends_paragraph(lines, i)
+ return lines[i]:match("^%s*$") or block_start:match(lines[i])
+ or table_starts(lines, i)
+ end
+
-- A paragraph's lines run together the way GitHub shows them, unless one
-- ends in two spaces or a backslash, which breaks the line there. The
-- last line of a paragraph has nothing to break before.
@@ -455,7 +468,7 @@ if has_lpeg then
lines = paragraph_lines(inner),
}
end
- elseif i + 1 <= n and line:find("|", 1, true) and table_delimiter:match(lines[i + 1]) then
+ elseif table_starts(lines, i) then
local head = {}
for _, cell in ipairs(split_cells(line)) do
head[#head + 1] = parse_inline(cell)
@@ -494,7 +507,7 @@ if has_lpeg then
else
local paragraph = { line }
i = i + 1
- while i <= n and not lines[i]:match("^%s*$") and not block_start:match(lines[i]) do
+ while i <= n and not ends_paragraph(lines, i) do
paragraph[#paragraph + 1] = lines[i]
i = i + 1
end
diff --git a/tests/extensions/test-about-render.lua b/tests/extensions/test-about-render.lua
index b7d12c7..6644664 100644
--- a/tests/extensions/test-about-render.lua
+++ b/tests/extensions/test-about-render.lua
@@ -216,6 +216,9 @@ out = render("README.md", "| a | b |\n| --- | --- |\n| 1 | 2 |")
h.contains("a pipe table renders its head", out, "<thead><tr><th>a</th><th>b</th></tr></thead>")
h.contains("a pipe table renders its body", out, "<tbody><tr><td>1</td><td>2</td></tr></tbody>")
+out = render("README.md", "Pick one:\n| a | b |\n|---|---|\n| 1 | 2 |")
+h.contains("a table cuts a paragraph short", out, "<p>Pick one:</p><table><thead><tr><th>a</th>")
+
out = render("README.md", "a | b in prose")
h.excludes("a pipe without a delimiter row stays prose", out, "<table")