raindown 0.17.2 → 0.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,22 +1,43 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'unicode/display_width'
3
+ require_relative 'table_column'
4
4
 
5
5
  module Raindown
6
- # Pads every column of each Markdown table to the width of its widest cell.
6
+ # Pads every column of each Markdown table to the width of its widest cell, but for a column whose delimiter is
7
+ # equals signs, like |====|, which is as wide as they are, letting a longer cell run past it. A row starting with |+,
8
+ # like "|+more | |", carries on the row above it, which Raindown joins into its cells when it renders. Its |+ takes
9
+ # the place of a row's "| ", so its columns line up.
7
10
  class TableFormatter
8
- DELIMITER_CELL = /\A:?-+:?\z/
11
+ DELIMITER_CELL = /\A:?[-=]+:?\z/
9
12
  FENCE = /\A\s*(```|~~~)/
13
+ CONTINUATION = /\A\s*\|\+/
10
14
 
11
15
  def self.rewrite(content:, **)
12
16
  new(content:).format
13
17
  end
14
18
 
19
+ # The Markdown as Commonmarker reads it: each table's equals signs as dashes, and its + rows joined into the row
20
+ # above them, each cell carrying on with a space.
21
+ def self.gfm(markdown)
22
+ new(content: markdown).gfm
23
+ end
24
+
15
25
  def initialize(content:)
16
26
  @lines = content.lines
17
27
  end
18
28
 
19
29
  def format
30
+ replace_tables { align(it) }
31
+ end
32
+
33
+ def gfm
34
+ replace_tables { gfm_table(it) }
35
+ end
36
+
37
+ private
38
+
39
+ # The content with each table, outside code, replaced by the block's lines for it.
40
+ def replace_tables
20
41
  output = []
21
42
  index = 0
22
43
  in_fence = false
@@ -27,7 +48,7 @@ module Raindown
27
48
 
28
49
  if !in_fence && table_start?(index)
29
50
  table_end = find_table_end(index)
30
- output.concat(align(@lines[index...table_end]))
51
+ output.concat(yield(@lines[index...table_end]))
31
52
  index = table_end
32
53
  else
33
54
  output << line
@@ -38,12 +59,26 @@ module Raindown
38
59
  output.join
39
60
  end
40
61
 
41
- private
62
+ # Split on pipes that aren't escaped, dropping the optional outer pipes, or the |+ starting a continuation row.
63
+ def cells(line)
64
+ content = line.strip
65
+ content = content.delete_prefix(row_lead(line).rstrip)
66
+ content = content.delete_suffix('|') unless content.end_with?('\|')
67
+
68
+ content.split(/(?<!\\)\|/, -1).map(&:strip)
69
+ end
70
+
71
+ # How the row's line starts, before its first cell: |+ when it carries on the row above, which is as wide as "| ".
72
+ def row_lead(line)
73
+ return '|+' if line.match?(CONTINUATION)
74
+
75
+ '| '
76
+ end
42
77
 
43
78
  def table_start?(index)
44
79
  header = @lines[index]
45
80
  delimiter = @lines[index + 1]
46
- return false unless header&.include?('|') && delimiter
81
+ return false unless header&.include?('|') && delimiter && !header.match?(CONTINUATION)
47
82
 
48
83
  delimiter_cells = cells(delimiter)
49
84
  delimiter_cells.any? && delimiter_cells.all? { |cell| cell.match?(DELIMITER_CELL) } &&
@@ -56,79 +91,40 @@ module Raindown
56
91
  table_end
57
92
  end
58
93
 
59
- def align(table)
60
- indent = table.first[/\A\s*/]
61
- rows = table.map { |line| cells(line) }
62
- header, delimiter, *body = rows
63
- return table if body.any? { |row| row.length > header.length }
64
-
65
- table_lines(indent:, header:, body:, alignments: delimiter.map { |cell| alignment(cell) })
94
+ def gfm_table(table)
95
+ header, delimiter, *body = table.map { |line| cells(line) }
96
+ rows = joined_rows(table.drop(2), header, body)
97
+ rows.insert(1, delimiter.map { it.tr('=', '-') }).map { "#{table.first[/\A\s*/]}| #{it.join(' | ')} |\n" }
66
98
  end
67
99
 
68
- def table_lines(indent:, header:, body:, alignments:)
69
- body = body.map { |row| row + ([''] * (header.length - row.length)) }
70
- widths = column_widths(rows: [header, *body])
71
-
72
- [
73
- row_line(indent:, cells: header, widths:, alignments:),
74
- delimiter_line(indent:, widths:, alignments:),
75
- *body.map { |row| row_line(indent:, cells: row, widths:, alignments:) }
76
- ].map { |line| "#{line}\n" }
77
- end
78
-
79
- # Split on pipes that aren't escaped, dropping the optional outer pipes.
80
- def cells(line)
81
- content = line.strip
82
- content = content.delete_prefix('|')
83
- content = content.delete_suffix('|') unless content.end_with?('\|')
84
-
85
- content.split(/(?<!\\)\|/, -1).map(&:strip)
86
- end
100
+ # The rows, each with the + rows below it joined into its cells.
101
+ def joined_rows(lines, header, body)
102
+ lines.zip(body).each_with_object([header]) do |(line, row), joined|
103
+ next joined << row unless line.match?(CONTINUATION)
87
104
 
88
- def alignment(cell)
89
- if cell.start_with?(':') && cell.end_with?(':') then :center
90
- elsif cell.end_with?(':') then :right
91
- elsif cell.start_with?(':') then :left
105
+ joined[-1] = joined.last.zip(row).map { |above, more| [above, more].reject { it.to_s.empty? }.join(' ') }
92
106
  end
93
107
  end
94
108
 
95
- # Three is the shortest delimiter cell, which also fits ':-:'.
96
- def column_widths(rows:)
97
- rows.transpose.map { |column| [column.map { |cell| display_width(cell) }.max, 3].max }
98
- end
99
-
100
- # Wide characters like CJK and emoji take two columns. Emoji are always measured as two,
101
- # instead of guessing from the terminal, so a file formats the same on every machine.
102
- def display_width(cell)
103
- Unicode::DisplayWidth.of(cell, emoji: :all)
104
- end
109
+ def align(table)
110
+ header, delimiter, *body = table.map { |line| cells(line) }
111
+ return table if body.any? { |row| row.length > header.length }
105
112
 
106
- def row_line(indent:, cells:, widths:, alignments:)
107
- padded = cells.each_with_index.map { |cell, i| pad(cell, widths[i], alignments[i]) }
108
- "#{indent}| #{padded.join(' | ')} |"
113
+ rows = [header, *body.map { |row| row + ([''] * (header.length - row.length)) }]
114
+ aligned_lines(table, rows, TableColumn.all(delimiter, rows))
109
115
  end
110
116
 
111
- def delimiter_line(indent:, widths:, alignments:)
112
- dashes = widths.each_with_index.map do |width, i|
113
- case alignments[i]
114
- when :center then ":#{'-' * (width - 2)}:"
115
- when :right then "#{'-' * (width - 1)}:"
116
- when :left then ":#{'-' * (width - 1)}"
117
- else '-' * width
118
- end
117
+ def aligned_lines(table, rows, columns)
118
+ indent = table.first[/\A\s*/]
119
+ lines = rows.zip(table.values_at(0, 2...table.length)).map do |row, line|
120
+ row_line(indent, line, columns.zip(row).map { |column, cell| column.pad(cell) })
119
121
  end
120
-
121
- "#{indent}| #{dashes.join(' | ')} |"
122
+ lines.insert(1, row_line(indent, table[1], columns.map(&:delimiter_cell)))
122
123
  end
123
124
 
124
- def pad(cell, width, alignment)
125
- padding = width - display_width(cell)
126
-
127
- case alignment
128
- when :center then "#{' ' * (padding / 2)}#{cell}#{' ' * (padding - (padding / 2))}"
129
- when :right then "#{' ' * padding}#{cell}"
130
- else "#{cell}#{' ' * padding}"
131
- end
125
+ # The row's line, starting with |+ when it carries on the row above, as the line it was did.
126
+ def row_line(indent, line, cells)
127
+ "#{indent}#{row_lead(line)}#{cells.join(' | ')} |\n"
132
128
  end
133
129
  end
134
130
  end
@@ -2,32 +2,47 @@
2
2
 
3
3
  require 'unicode/display_width'
4
4
 
5
+ require_relative 'file_cell'
5
6
  require_relative 'markdown_table'
6
- require_relative 'table_formulas'
7
+ require_relative 'matched_rows'
8
+ require_relative 'table_column'
7
9
 
8
10
  module Raindown
9
- # A table: block's table as it was written, its cells padded as they were, against its file's rows. A formula cell
10
- # shows its formula, so it can be edited, when its value's padding was deleted, no longer the width of its column's
11
- # header, and keeps showing it, until it's changed from the file's, ignoring whitespace.
11
+ # A table: block's table as it was written, its cells padded as they were, against its file's cells, as its columns
12
+ # show them. Each of its rows is matched to the file's row it shows, so a row added or removed above a formula
13
+ # doesn't move it onto another. A formula's value is no longer lined up when its padding's deleted, the width of its
14
+ # column's header.
12
15
  class WrittenTable
13
- def initialize(padded_rows, file)
16
+ # The delimiter's cells, like ':===', as it was written, or nil when it's not a table yet.
17
+ attr_reader :delimiter
18
+
19
+ def initialize(padded_rows, file, delimiter = nil)
14
20
  @padded_rows = padded_rows
15
21
  @file = file
16
22
  @values = TableFormulas.evaluate(file)
23
+ @delimiter = delimiter
24
+ @columns = TableColumn.all(delimiter, file)
17
25
  end
18
26
 
19
27
  # Its rows, as wide as each other, each formula still shown as it's saved being its value, as they're compared with the file's.
20
28
  def rows
21
- cells.each_with_index.map do |row, row_index|
22
- row.each_with_index.map do |cell, column|
23
- next @values.dig(row_index, column) if same_formula?(cell, @file.dig(row_index, column))
29
+ each_cell { |cell, row, column| file_cell(row, column)&.value_from(cell) || cell }
30
+ end
31
+
32
+ # Its rows to write to the file, keeping the file's cell where the table still shows it, like a formula as its
33
+ # value, or a cell with line breaks on one line.
34
+ def saved_rows
35
+ rows.each_with_index.map do |cells, row|
36
+ cells.each_with_index.map do |cell, column|
37
+ kept = file_cell(row, column)
38
+ next kept.saved if kept && MarkdownTable.one_line(kept.value) == cell
24
39
 
25
40
  cell
26
41
  end
27
42
  end
28
43
  end
29
44
 
30
- # The row and column of each cell to show as its formula.
45
+ # The row and column of each cell to show as its formula, or in full.
31
46
  def revealed
32
47
  cells.each_with_index.flat_map do |row, row_index|
33
48
  row.each_index.select { reveal?(row_index, it) }.map { [row_index, it] }
@@ -40,21 +55,39 @@ module Raindown
40
55
  @cells ||= @padded_rows.map { |row| Array.new(width_in_cells) { MarkdownTable.cell(row[it].to_s) } }
41
56
  end
42
57
 
58
+ def each_cell
59
+ cells.each_with_index.map { |row, row_index| row.each_with_index.map { |cell, column| yield cell, row_index, column } }
60
+ end
61
+
43
62
  # As wide as its widest row, as its file's rows are read, so a row still being typed reads the same in both.
44
63
  def width_in_cells
45
64
  @padded_rows.map(&:length).max
46
65
  end
47
66
 
48
- def reveal?(row, column)
49
- formula = @file.dig(row, column)
50
- return false unless TableFormulas.formula?(formula)
67
+ def file_cell(row, column)
68
+ file_rows[row]&.then { cell_in_file(it, column) }
69
+ end
51
70
 
52
- cell = cells[row][column]
53
- same_formula?(cell, formula) || (cell == @values.dig(row, column) && squeezed?(row, column))
71
+ def cell_in_file(file_row, column)
72
+ saved = @file.dig(file_row, column)
73
+ FileCell.new(saved:, value: @values.dig(file_row, column), column: @columns[column]) if saved
54
74
  end
55
75
 
56
- def same_formula?(cell, formula)
57
- TableFormulas.formula?(formula) && cell.gsub(/\s+/, '') == formula.gsub(/\s+/, '')
76
+ # The index of each of its rows' file row, or nil for a row that's new.
77
+ def file_rows
78
+ @file_rows ||= MatchedRows.new(cells.length, @file.length) { |row, file_row| same_row?(row, file_row) }.file_rows
79
+ end
80
+
81
+ # Whether the table's row shows the file's row, each cell unchanged, or blank where the file has none.
82
+ def same_row?(row, file_row)
83
+ cells[row].each_with_index.all? do |cell, column|
84
+ cell_in_file(file_row, column)&.unchanged_by?(cell) || (cell.empty? && !@file.dig(file_row, column))
85
+ end
86
+ end
87
+
88
+ def reveal?(row, column)
89
+ padded = @padded_rows[row][column].to_s
90
+ file_cell(row, column)&.revealed_by?(cells[row][column], squeezed: squeezed?(row, column), wrapped: padded.include?("\n"))
58
91
  end
59
92
 
60
93
  def squeezed?(row, column)
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../front_matter'
4
+ require_relative '../variables'
5
+
6
+ module Raindown
7
+ # The headings of Markdown as it's written, without rendering it, so a table of contents can be written back while
8
+ # it's typed: # headings and underlined ones, outside front matter, code and HTML comments, with their text as it
9
+ # reads, without its links, code or emphasis marks, and ids made the way Commonmarker makes them, numbered when
10
+ # they repeat, like to-read and to-read-1. Variables in them, like {title}, are filled in from the variables given,
11
+ # but headings in if: and for: blocks are as they're written, and headings that components render aren't in it, as
12
+ # they're only in HTML.
13
+ module MarkdownHeadings
14
+ ATX = /\A {0,3}(\#{1,6})(?:[ \t]+(.*?))?(?:[ \t]+\#+)?[ \t]*\z/
15
+ SETEXT = /\A {0,3}(=+|-+)[ \t]*\z/
16
+ FENCE = /\A {0,3}(`{3,}|~{3,})/
17
+ # A line that's a block of its own, rather than a paragraph's, which can't be underlined into a heading.
18
+ BLOCK = /\A {0,3}(?:[|>#<]|[-*+][ \t]|\d+[.)][ \t]|\z)/
19
+ COMMENT = /<!--.*?-->/m
20
+
21
+ module_function
22
+
23
+ def scan(markdown, variables: nil)
24
+ seen = Hash.new(0)
25
+ headings(lines(markdown)).map do |level, written|
26
+ written = filled(written, variables)
27
+ text = plain(written)
28
+ id = anchor(plain(written, images: true))
29
+ count = seen[id]
30
+ seen[id] += 1
31
+ { level:, text:, id: count.zero? ? id : "#{id}-#{count}" }
32
+ end
33
+ end
34
+
35
+ # A heading's id, the way Commonmarker makes it, like to-read for "To read".
36
+ def anchor(heading)
37
+ heading.downcase.gsub(/[^\p{Word}\- ]/, '').tr(' ', '-')
38
+ end
39
+
40
+ # Its lines after its front matter, with its HTML comments blanked out, keeping their lines.
41
+ def lines(markdown)
42
+ _, body = FrontMatter.parse(markdown)
43
+ body.to_s.gsub(COMMENT) { it.gsub(/[^\n]/, '') }.lines(chomp: true)
44
+ end
45
+
46
+ # Each heading's level and text, outside fenced code, in order.
47
+ def headings(lines)
48
+ fence = nil
49
+ lines.each_with_index.filter_map do |line, index|
50
+ marker = line[FENCE, 1]
51
+ if marker || fence
52
+ fence = fence_after(fence, marker)
53
+ next
54
+ end
55
+
56
+ heading(line, index.positive? ? lines[index - 1] : '')
57
+ end
58
+ end
59
+
60
+ # The fence still open after a line with the marker: closed by one like it, at least as long.
61
+ def fence_after(fence, marker)
62
+ return marker unless fence
63
+ return nil if marker&.start_with?(fence[0]) && marker.length >= fence.length
64
+
65
+ fence
66
+ end
67
+
68
+ def heading(line, above)
69
+ if (match = line.match(ATX))
70
+ [match[1].length, match[2].to_s]
71
+ elsif line.match?(SETEXT) && !above.match?(BLOCK) && !above.match?(SETEXT)
72
+ [line.strip.start_with?('=') ? 1 : 2, above]
73
+ end
74
+ end
75
+
76
+ # The heading with its variables filled in, like {title}.
77
+ def filled(text, variables)
78
+ return text unless variables
79
+
80
+ text.gsub(/\{([A-Za-z_][\w.]*)\}/) { Variables.value(variables, ::Regexp.last_match(1)).to_s }
81
+ end
82
+
83
+ # The heading's text as it reads: links as their text, images left out, or as their alt text, as in its id, code
84
+ # without its backticks, emphasis without its marks, but for underscores inside words, HTML without its tags, and
85
+ # escaped characters as themselves.
86
+ def plain(text, images: false)
87
+ text.strip
88
+ .gsub(/!\[([^\]]*)\]\([^)]*\)/) { images ? ::Regexp.last_match(1) : '' }
89
+ .gsub(/\[([^\]]*)\]\([^)]*\)/, '\1')
90
+ .gsub(/(`+)(.+?)\1/) { ::Regexp.last_match(2).strip }
91
+ .gsub(/(\*\*|\*)(?=\S)(.+?)(?<=\S)\1/, '\2')
92
+ .gsub(/(?<![[:alnum:]])(__|_)(?=\S)(.+?)(?<=\S)\1(?![[:alnum:]])/, '\2')
93
+ .gsub(/<[^>]*>/, '')
94
+ .gsub(/\\([[:punct:]])/, '\1')
95
+ .strip
96
+ end
97
+ end
98
+ end
@@ -2,7 +2,8 @@
2
2
 
3
3
  require 'cgi/escape'
4
4
 
5
- require_relative 'node'
5
+ require_relative 'markdown_headings'
6
+ require_relative '../node'
6
7
 
7
8
  module Raindown
8
9
  # The node behind toc: and :toc, a table of contents of the document's headings.
@@ -14,8 +15,10 @@ module Raindown
14
15
  IGNORED = %r{<!--.*?-->|<script\b.*?</script\s*>}mi
15
16
  TAG = /<[^>]*>/
16
17
 
18
+ # From the Markdown as it's written, without rendering it, so it's quick enough to write back as it's typed.
17
19
  def rewrite(document:, metadata: nil, bare: false)
18
- headings = headings_below_title(markdown: document, metadata:, bare:)
20
+ variables = Context.new(document:, metadata: metadata || NoMetadata, bare:).variables
21
+ headings = below_title(MarkdownHeadings.scan(document, variables:))
19
22
  return '' if headings.empty?
20
23
 
21
24
  min_level = headings.map { |heading| heading[:level] }.min
@@ -27,7 +30,7 @@ module Raindown
27
30
 
28
31
  # With the document's final HTML, the headings include ones its components render.
29
32
  def render(document:, html: nil, metadata: nil, bare: false)
30
- items = headings_below_title(markdown: document, html:, metadata:, bare:).map do |heading|
33
+ items = below_title(headings(markdown: document, html:, metadata:, bare:)).map do |heading|
31
34
  "<li class='h#{heading[:level]}'><a href='##{heading[:id]}'>#{CGI.escapeHTML(heading[:text])}</a></li>"
32
35
  end
33
36
 
@@ -44,8 +47,8 @@ module Raindown
44
47
  end
45
48
 
46
49
  # A lone top-level heading that starts the document is its title, so it's left out when there are headings below it.
47
- def headings_below_title(**)
48
- title, *below = headings = headings(**)
50
+ def below_title(headings)
51
+ title, *below = headings
49
52
  return headings if below.empty? || title[:level] != 1 || below.any? { it[:level] == 1 }
50
53
 
51
54
  below
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'csv'
3
+ # Loaded when first used, as it loads strscan, a native extension, which slows a packed executable's start.
4
+ autoload :CSV, 'csv'
4
5
  require 'fileutils'
5
6
  require 'open3'
6
7
  require 'time'
@@ -7,7 +7,7 @@ require_relative '../document'
7
7
  require_relative '../front_matter'
8
8
  require_relative '../node_blocks'
9
9
  require_relative '../nodes'
10
- require_relative '../pages'
10
+ require_relative '../pages/pages'
11
11
  require_relative '../projects'
12
12
  require_relative '../workers/workers'
13
13
  require_relative '../stat'
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative 'code'
4
4
  require_relative 'front_matter'
5
+ require_relative 'toc/markdown_headings'
5
6
 
6
7
  module Raindown
7
8
  # Obsidian's links between notes, which render as Markdown links to the file they name, by its path in the project,
@@ -53,14 +54,9 @@ module Raindown
53
54
  def markdown_link(link, page)
54
55
  name, heading, label = link.captures.map { it&.strip }
55
56
  label ||= [name, heading].compact.join(' > ')
56
- anchor = "##{anchor(heading)}" if heading
57
+ anchor = "##{MarkdownHeadings.anchor(heading)}" if heading
57
58
 
58
59
  "[#{label.gsub(/[\[\]]/) { "\\#{it}" }}](<#{page.path}#{anchor}>)"
59
60
  end
60
-
61
- # A heading's id, the way Commonmarker makes it, like to-read for "To read".
62
- def anchor(heading)
63
- heading.downcase.gsub(/[^\p{Word}\- ]/, '').tr(' ', '-')
64
- end
65
61
  end
66
62
  end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raindown
4
+ # What a node's rewrite writes below its directive: its Markdown, and a comment straight below the directive, like
5
+ # table:'s "2 rows · 3f2a9c", which its rewrite gets back as comment: the next time it writes. A rewrite can return a
6
+ # string instead, when it has no comment.
7
+ class Written
8
+ attr_reader :markdown, :comment
9
+
10
+ def initialize(markdown, comment: nil)
11
+ @markdown = markdown
12
+ @comment = comment
13
+ end
14
+
15
+ # Its Markdown, as a rewrite's string would be, like when it's rendered as HTML.
16
+ def to_s
17
+ markdown.to_s
18
+ end
19
+ end
20
+ end
data/lib/raindown.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'commonmarker'
4
3
  require 'antlers'
5
4
  require 'antlers/api'
6
5
 
@@ -14,8 +13,9 @@ require_relative 'raindown/fragments'
14
13
  require_relative 'raindown/front_matter'
15
14
  require_relative 'raindown/heading_ids'
16
15
  require_relative 'raindown/node_blocks'
17
- require_relative 'raindown/pages'
16
+ require_relative 'raindown/pages/pages'
18
17
  require_relative 'raindown/pre_renderer'
18
+ require_relative 'raindown/table/table_formatter'
19
19
  require_relative 'raindown/utils'
20
20
  require_relative 'raindown/variables'
21
21
  require_relative 'raindown/wiki_links'
@@ -49,31 +49,39 @@ module Raindown
49
49
  # Front matter keys are variables, like {title} or <{ title }>.
50
50
  # Bare directives like "toc:" show in bold in the HTML, and the rest stay hidden, as comments.
51
51
  # Code blocks are highlighted, unless highlight_code is false for a site that highlights code in the browser.
52
- def render(markdown:, metadata: NoMetadata, bare: false, highlight_code: true)
52
+ # Without commonmark, it's Markdown, not HTML, with nodes as they'd be written back, for an app to render itself, like
53
+ # Obsidian's reading view in the plugin, where Commonmarker, which is native, can't run.
54
+ def render(markdown:, metadata: NoMetadata, bare: false, highlight_code: true, commonmark: true)
53
55
  context = Context.new(document: markdown, metadata:, bare: bare || BareDirectives.front_matter?(markdown))
54
56
 
55
- highlighting(highlight_code) do
57
+ rendering(highlight_code:, commonmark:) do
56
58
  Fragments.collect do
57
59
  # Components' HTML goes back in, then their headings get unique ids, before nodes like the TOC see it.
58
60
  html = Nodes.render_placeholders(assembled(context), context)
61
+ output = Directives.render("#{html.chomp}\n")
62
+ next output if commonmark
59
63
 
60
- Directives.render("#{html.chomp}\n")
64
+ output.gsub(/[\u{E000}\u{E001}]/, CODE_BRACES.invert)
61
65
  end
62
66
  end
63
67
  end
64
68
 
65
- # Whether code is highlighted while the block runs, like one document's render,
69
+ # Whether code is highlighted, and Markdown made HTML, while the block runs, like one document's render,
66
70
  # so components and endpoints rendering their own Markdown follow it too.
67
- def highlighting(highlight_code)
68
- outer = Thread.current[:raindown_highlight_code]
69
- Thread.current[:raindown_highlight_code] = highlight_code
71
+ def rendering(highlight_code:, commonmark: true)
72
+ outer = Thread.current[:raindown_rendering]
73
+ Thread.current[:raindown_rendering] = { highlight_code:, commonmark: }
70
74
  yield
71
75
  ensure
72
- Thread.current[:raindown_highlight_code] = outer
76
+ Thread.current[:raindown_rendering] = outer
73
77
  end
74
78
 
75
79
  def highlight_code?
76
- Thread.current[:raindown_highlight_code] != false
80
+ Thread.current[:raindown_rendering]&.fetch(:highlight_code) != false
81
+ end
82
+
83
+ def commonmark?
84
+ Thread.current[:raindown_rendering]&.fetch(:commonmark) != false
77
85
  end
78
86
 
79
87
  # The document's HTML with its components' HTML put back, and unique heading ids.
@@ -87,9 +95,18 @@ module Raindown
87
95
  commonmark(PreRenderer.render(markdown, variables: context.variables, context:))
88
96
  end
89
97
 
90
- # Markdown to HTML, like a document's or a component's slot.
98
+ # Commonmarker's tree of the Markdown. It's only loaded here, as only rendering HTML needs it, so a build without it,
99
+ # like the WebAssembly one, can still write back.
100
+ def parse_markdown(markdown)
101
+ require 'commonmarker'
102
+ Commonmarker.parse(markdown, options: MARKDOWN_OPTIONS)
103
+ end
104
+
105
+ # Markdown to HTML, like a document's or a component's slot, unless it's rendering Markdown.
91
106
  def commonmark(markdown)
92
- doc = Commonmarker.parse(hide_directives(markdown:), options: MARKDOWN_OPTIONS)
107
+ return markdown unless commonmark?
108
+
109
+ doc = parse_markdown(hide_directives(markdown: TableFormatter.gfm(markdown)))
93
110
  preserve_code(doc:)
94
111
 
95
112
  html = highlight_code? ? doc.to_html(options: HTML_OPTIONS) : unhighlighted_html(doc:)
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Raindown
4
- VERSION = '0.17.2'
4
+ VERSION = '0.19.0'
5
5
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: raindown
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.17.2
4
+ version: 0.19.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - maedi
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2026-11-08 00:00:00.000000000 Z
10
+ date: 2026-11-10 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: antlers
@@ -228,6 +228,7 @@ files:
228
228
  - lib/raindown/document.rb
229
229
  - lib/raindown/editing.rb
230
230
  - lib/raindown/endpoint/curl.rb
231
+ - lib/raindown/endpoint/curl_request.rb
231
232
  - lib/raindown/endpoint/curl_response.rb
232
233
  - lib/raindown/endpoint/endpoint_args.rb
233
234
  - lib/raindown/endpoint/endpoint_runner.rb
@@ -245,8 +246,9 @@ files:
245
246
  - lib/raindown/node_blocks.rb
246
247
  - lib/raindown/node_injector.rb
247
248
  - lib/raindown/nodes.rb
248
- - lib/raindown/page.rb
249
- - lib/raindown/pages.rb
249
+ - lib/raindown/pages/listings.rb
250
+ - lib/raindown/pages/page.rb
251
+ - lib/raindown/pages/pages.rb
250
252
  - lib/raindown/pre_renderer.rb
251
253
  - lib/raindown/problem.rb
252
254
  - lib/raindown/projects.rb
@@ -254,13 +256,17 @@ files:
254
256
  - lib/raindown/server.rb
255
257
  - lib/raindown/skeleton.rb
256
258
  - lib/raindown/stat.rb
259
+ - lib/raindown/table/file_cell.rb
257
260
  - lib/raindown/table/markdown_table.rb
261
+ - lib/raindown/table/matched_rows.rb
258
262
  - lib/raindown/table/table.rb
263
+ - lib/raindown/table/table_column.rb
259
264
  - lib/raindown/table/table_file.rb
260
265
  - lib/raindown/table/table_formatter.rb
261
266
  - lib/raindown/table/table_formulas.rb
262
267
  - lib/raindown/table/written_table.rb
263
- - lib/raindown/toc.rb
268
+ - lib/raindown/toc/markdown_headings.rb
269
+ - lib/raindown/toc/toc.rb
264
270
  - lib/raindown/tui.rb
265
271
  - lib/raindown/tui/blueprints/blueprints_renderer.rb
266
272
  - lib/raindown/tui/blueprints/blueprints_view.rb
@@ -339,6 +345,7 @@ files:
339
345
  - lib/raindown/wiki_links.rb
340
346
  - lib/raindown/workers/worker.rb
341
347
  - lib/raindown/workers/workers.rb
348
+ - lib/raindown/written.rb
342
349
  - lib/version.rb
343
350
  homepage: https://github.com/rainpour/raindown
344
351
  licenses: []
File without changes