Skip to content

parse table cell spans in cmark and use the information - #74

Merged
QuietMisdreavus merged 2 commits into
swiftlang:mainfrom
QuietMisdreavus:table-spans
Sep 21, 2022
Merged

parse table cell spans in cmark and use the information#74
QuietMisdreavus merged 2 commits into
swiftlang:mainfrom
QuietMisdreavus:table-spans

Conversation

@QuietMisdreavus

Copy link
Copy Markdown
Contributor

Bug/issue #, if applicable: rdar://98017807

Summary

This PR adds support for swift-cmark's new table row- and column-spanning syntax and enables it by default. (A syntax overview is written up in the linked PR's description.) The spanning values are exposed as properties on the Table.Cell struct, with the following semantics:

  • A value of 1 is the default, indicating that the cell is not spanning over other cells in that dimension.
  • A value of 0 indicates that the cell is being spanned over by another cell in that dimension, and thus should be omitted when rendering HTML.
  • A value greater than 1 indicates that the cell is spanning over that number of rows or columns.

Support has been added in Swift-Markdown's Markdown reformatter and its debug tree representation to properly process the new information.

Dependencies

swiftlang/swift-cmark#46 (already merged)

Testing

Use the following sample Markdown table, showcasing the new syntax:

| one | two | three |
| --- | --- | ----- |
| big      || small |
| ^        || small |

Steps:

  1. swift run markdown-tool dump-tree test.md
  2. Compare the output against the following dump:
Document
└─ Table alignments: |-|-|-|
   ├─ Head
   │  ├─ Cell
   │  │  └─ Text "one"
   │  ├─ Cell
   │  │  └─ Text "two"
   │  └─ Cell
   │     └─ Text "three"
   └─ Body
      ├─ Row
      │  ├─ Cell colspan: 2 rowspan: 2
      │  │  └─ Text "big"
      │  ├─ Cell colspan: 0
      │  └─ Cell
      │     └─ Text "small"
      └─ Row
         ├─ Cell colspan: 2 rowspan: 0
         ├─ Cell colspan: 0
         └─ Cell
            └─ Text "small"

Checklist

Make sure you check off the following items. If they cannot be completed, provide a reason.

  • Added tests
  • Ran the ./bin/test script and it succeeded
  • Updated documentation if necessary

@QuietMisdreavus

Copy link
Copy Markdown
Contributor Author

@swift-ci Please test

Comment on lines +983 to +994
let colspan = headCellSpans[column]
if colspan == 0 {
// If this cell is being spanned over, collapse it so it
// can be filled with the spanning cell.
return ""
} else {
// With a colspan, we want to expand the cell width to
// cover multiple columns. This helpfully generalizes
// to `colspan == 1`, where it will only query the
// current column!
let lastColumn = column + Int(colspan)
let minLineLength = (column..<lastColumn).map({ finalColumnWidths[$0] }).reduce(0, { $0 + $1 })

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

�Should this logic be refactored?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea! Just pushed up a new commit combining the logic.

@QuietMisdreavus

Copy link
Copy Markdown
Contributor Author

@swift-ci Please test

@daniel-grumberg
daniel-grumberg self-requested a review September 21, 2022 10:07
@QuietMisdreavus
QuietMisdreavus merged commit 395fbf2 into swiftlang:main Sep 21, 2022
@QuietMisdreavus
QuietMisdreavus deleted the table-spans branch September 21, 2022 15:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants