Skip to content

Commit

Permalink
feat: support even more page break markers (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
as-op authored Jul 10, 2024
1 parent 9b568dc commit 83c3541
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
10 changes: 7 additions & 3 deletions demo/demo.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,15 +340,19 @@ With a reference later in the document defining the URL location.

## Page breaks

use one of these formats to start a new page
use one of these formats to start a new page (all with mandatory empty lines around them)

`<br style="page-break-before: always;"/>`

`<br style="page-break-after: always;"/>`

`<div style="page-break-before: always;"/>`

`<br-page/>` (custom tag with mandatory empty line before other markdown content)
`<div style="page-break-after: always;"/>`

`<br-page/>`

`<page-br/>` (custom tag with mandatory empty line before other markdown content)
`<page-br/>`

---

Expand Down
2 changes: 1 addition & 1 deletion lib/md_to_pdf/elements/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def is_page_break_tag?(tag)
return false unless tag.key?('style')

style = tag.get_attribute('style') || ''
/page-break-before\s*:\s*always/ === style
/page-break-(before|after)\s*:\s*always/ === style
end

def find_img_caption(tag)
Expand Down
32 changes: 26 additions & 6 deletions spec/fixtures/page/breaks.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
First page <!-- (custom html tag NEEDS the empty line) -->
Page 1

<br-page/>
Second page <!-- (custom html tag NEEDS the empty line) -->

Page 2

<page-br/>
Third page <!-- (used by OpenProject and CKEditor) -->

Page 3

<br style="page-break-after: always;"/>

Page 4

<br style="page-break-before: always;"/>
Fourth page <!-- (used by many editors) -->

Page 5

<div style="page-break-before: always;">Everything here <br-page/> is ignored</div>
Fifth page <!-- (CKEditor default format) -->

Page 6

<div style="page-break-after: always;">Everything here <br-page/> is ignored</div>

Page 7

<div style="page-break-before: always;">Everything here <br-page/> is ignored</div>

Page 8

<div class="page-break" style="page-break-before: always;"><span style="display: none"></span></div>
Sixth page

Page 9
17 changes: 10 additions & 7 deletions spec/markdown_to_pdf/page_break_spec.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
require 'pdf_helpers'

describe MarkdownToPDF::Text do
describe MarkdownToPDF::Page do
include_context 'with pdf'

it 'creates page breaks' do
generator.parse_file('page/breaks.md')
expect_pdf([
{ x: 36.0, y: 747.384, text: "First page" },
{ x: 36.0, y: 747.384, text: "Second page" },
{ x: 36.0, y: 747.384, text: "Third page" },
{ x: 36.0, y: 747.384, text: "Fourth page" },
{ x: 36.0, y: 747.384, text: "Fifth page" },
{ x: 36.0, y: 747.384, text: "Sixth page" }])
{ x: 36.0, y: 747.384, text: "Page 1" },
{ x: 36.0, y: 747.384, text: "Page 2" },
{ x: 36.0, y: 747.384, text: "Page 3" },
{ x: 36.0, y: 747.384, text: "Page 4" },
{ x: 36.0, y: 747.384, text: "Page 5" },
{ x: 36.0, y: 747.384, text: "Page 6" },
{ x: 36.0, y: 747.384, text: "Page 7" },
{ x: 36.0, y: 747.384, text: "Page 8" },
{ x: 36.0, y: 747.384, text: "Page 9" }])
end
end

0 comments on commit 83c3541

Please sign in to comment.