Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tables support for Markdown #3589

Closed
wants to merge 1 commit into from
Closed

Add tables support for Markdown #3589

wants to merge 1 commit into from

Conversation

andrewzah
Copy link

This commit lets this syntax work for markdown:

- denotes a header, and : denotes alignment.

|:- Center Align -:|- Right Align -:|: Left Align -|- Default (left) |
| Row1-1 | Row1-2 | Row1-3 | Row1-4 |
| Row2-1 | Row2-2 | Row2-3 | Row2-4 |

This style saves the extra line of typical markdown syntax:

| Header |
|: --- |
| Row1-1 |

This is my first contribution, so if my implementation can be made better or more "Crystal-like" please let me know.

@sdogruyol
Copy link
Member

@azah welcome and congrulations on your first pull request 🎉

elsif element[0] == ':'
return "left"
else
return "left"
Copy link
Contributor

Choose a reason for hiding this comment

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

No need to return since it's implicit.

Copy link
Author

Choose a reason for hiding this comment

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

Updated.

Add spec
update for implicit returns
Change to text-align because table.align is deprecated
@andrewzah
Copy link
Author

andrewzah commented Nov 28, 2016

Updated, because apparently table.align is deprecated.

@samueleaton
Copy link
Contributor

At first I was thinking that this just GFM and doesn't need to be part of the Crystal markdown parser but seeing as...

  1. Many project READMEs will be in a Github repo
  2. The first page for a Crystal project doc will use the same README

...then we should support it just for the sake of crystal docs

@samueleaton
Copy link
Contributor

samueleaton commented Dec 19, 2016

This actually breaks for all of the examples on Github's page about tables

require "markdown"

md = "

# MyProject

## A Table

| First Header  | Second Header |
| ------------- | ------------- |
| Content Cell  | Content Cell  |
| Content Cell  | Content Cell  |

"

Markdown.to_html md
# Missing hash key: 0 (KeyError)

And I would add some more tests. I would test against the examples on that Github page I shared and also the Ruby Kramdown lib actually has a good collection of markdown text files and the expected HTML output.

@andrewzah
Copy link
Author

@samueleaton yeah I now realize github doesn't support the format I used, it appears they require the --- line to delineate between header / data cells. At the time I was just developing it for my own personal use.

I would have to rewrite the code then pretty much, so I will at some point.

@jhass
Copy link
Member

jhass commented Feb 12, 2017

Not sure we should support this, CommonMark only allows the HTML syntax, see http://spec.commonmark.org/0.27/ rule 4.6.6.

@0x1eef
Copy link

0x1eef commented Feb 12, 2017

@jhass well, GitHub flavoured markdown has supported PHP markdown tables for a while. Redcarpet and friends deal with this by being able to turn on or off unofficial extensions.

@RX14
Copy link
Contributor

RX14 commented Feb 12, 2017

I'd say that the best way to do this is to conform to commonmark by default but allow extensions to the markdown parser. This would probably require the markdown code to be rewritten to allow extensibility however.

@andrewzah
Copy link
Author

andrewzah commented Feb 12, 2017

CommonMark has no table support at all besides using raw html, which is both ugly and tedious compared to markdown's simplicity.

Kramdown, Github, Gitlab, and PHP Markdown Extra use this format for tables:

| header 1 | header 2 |
| -------- | -------- |
| cell 1   | cell 2   |
| cell 3   | cell 4   |

with left, center, or right alignment syntax as follows:

| Left | Center | Right |
| :--- | :---: | ---: |

They also also support this syntax:

header 1 | header 2
-------- | --------
cell 1   | cell 2
cell 3   | cell 4

I think that we use tables enough that they should be a core aspect, and we definitely use Github/Gitlab enough to give their syntax merit.

However, extensions would be a good idea, because of differences like Gitlab's emojis or diff syntax:

{+ additions +}
[+ additions +]
{- deletions -}
[- deletions -]

References:

@andrewzah andrewzah closed this Jul 17, 2017
@david50407
Copy link
Contributor

Maybe this can be a standalone shards for parsing more markdown dialects?

@andrewzah
Copy link
Author

andrewzah commented Jul 17, 2017

That's likely the best solution because custom extensions like {+ additions +} are useless without a parser that can understand it.

Unfortunately I am working full time as a teacher & studying Korean- if someone else is interested in working on this by all means go ahead. I'll eventually make a shard at some point in the future.

@akzhan
Copy link
Contributor

akzhan commented Jul 18, 2017

Crystal should support GFM by default because it's standard de facto. And it's great for API documentation etc.

Just look at https://toolchain.gitbook.com/syntax/markdown.html

@straight-shoota
Copy link
Member

Please keep the discussion about improving the markdown implementation in #4613

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.

None yet

10 participants