Skip to content

Commit

Permalink
Remove inline alignment from tables.
Browse files Browse the repository at this point in the history
  • Loading branch information
fnando committed Feb 1, 2024
1 parent 82f3625 commit c3622ad
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- Wrap emoji with `span.emoji` and use a font for emojis so it renders the same
in any system.
- Update all templates to use h1 as the root heading.
- Remove inline alignment from tables.

## v3.1.0

Expand Down
30 changes: 27 additions & 3 deletions lib/kitabu/markdown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class Renderer < Redcarpet::Render::HTML
# Support alert boxes just like github.
# https://github.com/orgs/community/discussions/16925
def block_quote(quote)
html = Nokogiri::HTML(quote)
element = html.css("body > :first-child").first
html = Nokogiri::HTML.fragment(quote)
element = html.children.first

matches = element.text.match(ALERT_MARK)

Expand All @@ -33,10 +33,34 @@ def block_quote(quote)
<<~HTML.strip_heredoc
<div class="note #{type}">
<p class="note--title">#{title}</p>
#{html.css('body').inner_html}
#{html}
</div>
HTML
end

def table_cell(content, alignment, header)
tag = header ? "th" : "td"

html = Nokogiri::HTML.fragment("<#{tag}></#{tag}>")
node = html.children.first
node.append_class("align-#{alignment}") if alignment
node.content = content

html.to_s
end

def table(header, body)
<<~HTML
<table class="table">
<thead>
#{header}
</thead>
<tbody>
#{body}
</tbody>
</table>
HTML
end
end

class << self
Expand Down
69 changes: 45 additions & 24 deletions spec/kitabu/markdown_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

describe Kitabu::Markdown do
it "enables fenced code blocks" do
html = Kitabu::Markdown.render <<-TEXT.strip_heredoc
html = Kitabu::Markdown.render <<~TEXT
```ruby
class User
end
Expand All @@ -15,41 +15,41 @@ class User
end

it "enables options" do
html = Kitabu::Markdown.render <<-TEXT.strip_heredoc
```php?start_inline=true
echo 'Hello';
```
html = Kitabu::Markdown.render <<~TEXT
```php?start_inline=true
echo 'Hello';
```
TEXT

expect(html).to include('<span class="k">echo</span>')
end

it "enables line numbers" do
html = Kitabu::Markdown.render <<-TEXT.strip_heredoc
```ruby?line_numbers=true
class User
end
```
html = Kitabu::Markdown.render <<~TEXT
```ruby?line_numbers=true
class User
end
```
TEXT

expect(html).to include(%[<table class="rouge-table">])
end

it "does not raise with unknown lexers" do
expect do
Kitabu::Markdown.render <<-TEXT.strip_heredoc
```terminal
Text plain.
```
Kitabu::Markdown.render <<~TEXT
```terminal
Text plain.
```
TEXT
end.not_to raise_error
end

it "renders alert boxes using block quotes" do
html = Kitabu::Markdown.render <<-TEXT.strip_heredoc
> [!NOTE]
>
> This is just a note
html = Kitabu::Markdown.render <<~TEXT
> [!NOTE]
>
> This is just a note
TEXT

html = Nokogiri::HTML(html)
Expand All @@ -60,10 +60,10 @@ class User
end

it "renders arbitrary alert boxes using block quotes" do
html = Kitabu::Markdown.render <<-TEXT.strip_heredoc
> [!ALERT]
>
> This is just an alert
html = Kitabu::Markdown.render <<~TEXT
> [!ALERT]
>
> This is just an alert
TEXT

html = Nokogiri::HTML(html)
Expand All @@ -74,8 +74,8 @@ class User
end

it "renders regular block quotes" do
html = Kitabu::Markdown.render <<-TEXT.strip_heredoc
> This is just a quote
html = Kitabu::Markdown.render <<~TEXT
> This is just a quote
TEXT

html = Nokogiri::HTML(html)
Expand All @@ -97,4 +97,25 @@ class User

expect(Kitabu::Markdown.render("Hello")).to eql("<em>BYE</em>\n")
end

it "replaces inline style of table alignment" do
html = Kitabu::Markdown.render <<~TEXT
| Month | Savings | Type | Note |
| :-------- | -------: | :-------:| -----|
| January | $250 | Positive | N/A |
TEXT

html = Nokogiri::HTML(html)

expect(html).to have_tag("table.table")
expect(html).to have_tag("table>thead>tr>th.align-left", "Month")
expect(html).to have_tag("table>thead>tr>th.align-right", "Savings")
expect(html).to have_tag("table>thead>tr>th.align-center", "Type")
expect(html).to have_tag("table>thead>tr>th:not([class*='align'])", "Note")

expect(html).to have_tag("table>tbody>tr>td.align-left", "January")
expect(html).to have_tag("table>tbody>tr>td.align-right", "$250")
expect(html).to have_tag("table>tbody>tr>td.align-center", "Positive")
expect(html).to have_tag("table>tbody>tr>td:not([class*='align'])", "N/A")
end
end

0 comments on commit c3622ad

Please sign in to comment.