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

Improved support of TABLE #31

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
48aa859
Update tables.js
Apr 15, 2021
dfc18f6
Fixed table that can contain colgroup or caption
Apr 15, 2021
0e1ef9d
Adjusted syntax
Apr 18, 2021
e44319a
Upgraded turndown and associated pkg versions
Apr 21, 2021
56f0662
More test cases
Apr 21, 2021
3495630
Minor cleanup
Apr 21, 2021
db72828
Fix table with no row
Apr 21, 2021
9797a2e
Added name of IIFE config
May 3, 2021
3b497bb
Removed name from rollup config
May 3, 2021
36a0e43
Updated to node v5 (prepublish deprecated)
May 3, 2021
ebc6a02
Many more test cases
May 3, 2021
37c0d69
Support for colspan, caption, and any table (no header selection)
May 3, 2021
79c0c0b
New install
May 3, 2021
b82d5e6
Added windows new line support, expanded test case for colspan, fixed…
guyplusplus Jun 19, 2021
8f9fa66
New HTML test page
guyplusplus Jun 20, 2021
adba171
More test cases for empty cells in dows
guyplusplus Jun 20, 2021
c3d12a0
adjusted version and package name until pull request is approved...
guyplusplus Jun 20, 2021
3ab0f7e
More information on the fork
guyplusplus Jun 20, 2021
21e3fdd
added link to test page
guyplusplus Jun 20, 2021
918d1f4
Fix usage in document of init of turndown-plugin-gfm
guyplusplus Jun 20, 2021
68826e6
fixed npm reference to point to fork
guyplusplus Jun 21, 2021
2ea0110
section rename
guyplusplus Jun 21, 2021
866f7dc
upgrade to 1.0.5 as it conflicts with npmjs pkg (my mistake)
guyplusplus Jun 21, 2021
1a755a3
adjust turndown dependency
guyplusplus Jun 22, 2021
d6a732e
adjusted example
guyplusplus Jun 22, 2021
9c8e166
upgrade turndown lib to 7.1.1
guyplusplus Jul 3, 2021
4e033dc
bump minimist to 1.2.6
Apr 9, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,53 @@

A [Turndown](https://github.com/domchristie/turndown) plugin which adds GitHub Flavored Markdown extensions.

## Test Page

The test page can be found [here](https://guyplusplus.github.io/turndown-plugin-gfm/).

## Fork information

This is a fork of the original [turndown-plugin-gfm](https://github.com/domchristie/turndown-plugin-gfm) for general purpose. It is pending on this [pull request](https://github.com/domchristie/turndown-plugin-gfm/pull/31).

The changes are:
- migrated to node v5
- upgraded inclusion of turndown npm package to 7.0.0
- support tables with `caption` and `colgroup` elements
- support conversion of a multi-lines cell to a single line cell
- pipe character `|` are properly escaped in a table cell
- fix support for tables with no row
- support for `colspan`
- nested table inside a table render as is, gfm is not applied
- added more test cases accordingly

TODO:
- [ ] rowspan support
- [ ] support for relaxed and strict gfm (not html) rendering. So new lines, nested tables would render differently

## Installation

npm:

```
npm install turndown-plugin-gfm
npm install @guyplusplus/turndown-plugin-gfm
```

Browser:

```html
<script src="https://unpkg.com/turndown/dist/turndown.js"></script>
<script src="https://unpkg.com/turndown-plugin-gfm/dist/turndown-plugin-gfm.js"></script>
<script src="https://unpkg.com/@guyplusplus/turndown-plugin-gfm/dist/turndown-plugin-gfm.js"></script>
```

## Usage

```js
// For Node.js
var TurndownService = require('turndown')
var turndownPluginGfm = require('turndown-plugin-gfm')
var TurndownPluginGfm = require('@guyplusplus/turndown-plugin-gfm')

var gfm = turndownPluginGfm.gfm
var turndownService = new TurndownService()
turndownService.use(gfm)
TurndownPluginGfm.gfm(turndownService)
var markdown = turndownService.turndown('<strike>Hello world!</strike>')
```

Expand Down
3 changes: 2 additions & 1 deletion config/rollup.config.iife.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import config from './rollup.config'
export default config({
output: {
format: 'iife',
file: 'dist/turndown-plugin-gfm.js'
file: 'dist/turndown-plugin-gfm.js',
name: 'TurndownPluginGfmService'
}
})
1 change: 0 additions & 1 deletion config/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export default function (config) {
return {
name: 'turndownPluginGfm',
input: 'src/gfm.js',
output: config.output
}
Expand Down
266 changes: 266 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Turndown Plugin GFM Demo</title>
<meta name="viewport" content="width=device-width">
<style>
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}

body {
margin: 0 auto;
font-family: courier, monospace, sans-serif;
line-height: 1.3125;
color: #333;
}

header {
padding: 0.5em;
background-color: #fff;
text-align: center;
}

footer {
font-size: 0.875em;
text-align: center;
color: #666;
}

h1,
label[for="input"],
label[for="output"] {
margin: 0;
font-size: 1em;
font-weight: 700;
letter-spacing: 0.0625em;
text-transform: uppercase;
}

label[for="input"],
label[for="output"] {
font-weight: 400;
}

.source-link {
font-size: 0.875em;
}

.row:before,
.row:after {
content: " ";
display: table;
}

.row:after {
clear: both;
}

.row {
*zoom: 1;
}

.col,
.form-group {
padding: 0 0.5em;
}

@media (min-width: 36em) {
.col {
float: left;
width: 50%;
}
}

textarea {
width: 100%;
height: 36em;
margin: 0;
padding: 0.5em;
overflow: auto;
border: 1px solid;
background-color: #fff;
font-family: inherit;
font-size: inherit;
line-height: inherit;
color: inherit;
}

#input {
background-color: #333;
color: #fff;
border: none;
}

select {
display: block;
width: 100%;
font-size: inherit;
}

.form-group {
display: inline-block;
}

.form-group label {
font-size: 0.875em;
}
</style>
<script src="https://unpkg.com/turndown/dist/turndown.js"></script>
<script src="https://unpkg.com/@guyplusplus/turndown-plugin-gfm/dist/turndown-plugin-gfm.js"></script>
</head>
<body>
<header>
<h1>Turndown Plugin GFM Demo</h1>
<a class="source-link" href="https://github.com/guyplusplus/turndown-plugin-gfm">Source on GitHub</a>
</header>
<div class="row">
<div class="col">
<label for="input">HTML</label>
<textarea cols="100" rows=10 id="input"><h1>Turndown Plugin GFM Demo</h1>

<p>This demonstrates <a href="https://github.com/domchristie/turndown">turndown</a> – an HTML to Markdown converter in JavaScript with additional support for GFM via the <a href="https://github.com/guyplusplus/turndown-plugin-gfm">turndown-plugin-gfm</a> plugin.</p>

<h2>Usage</h2>

<pre><code class="language-js">var turndownService = new TurndownService()
TurndownPluginGfmService.gfm(turndownService)
console.log(
turndownService.turndown('&amp;lt;strike&amp;gt;Hello world&amp;lt;/strike&amp;gt;')
)</code></pre>

<hr />

<p>It aims to be <a href="http://commonmark.org/">CommonMark</a>
compliant, and includes additional options to style the output. These options include:</p>

<ul>
<li>strikethrough (~)</li>
<li>tables (|, |---|)</li>
<li>task list (- [ ] or - [x])</li>
</ul></textarea>
</div>
<div class="col">
<label for="output">GFM (GitHub Flavored Markdown)</label>
<textarea readonly cols="100" rows=10 id="output"></textarea>
</div>
</div>

<div class="row">
<form method="get" action="/turndown" id="options">
<div class="form-group">
<label for="headingStyle">Heading style</label>
<select name="headingStyle" id="headingStyle">
<option value="setext">setext</option>
<option value="atx">atx</option>
</select>
</div>

<div class="form-group">
<label for="hr">Horizontal rule</label>
<select name="hr" id="hr">
<option value="* * *">* * *</option>
<option value="- - -">- - -</option>
<option value="_ _ _">_ _ _</option>
</select>
</div>

<div class="form-group">
<label for="bulletListMarker">Bullet</label>
<select name="bulletListMarker" id="bulletListMarker">
<option value="*">*</option>
<option value="-">-</option>
<option value="+">+</option>
</select>
</div>

<div class="form-group">
<label for="codeBlockStyle">Code block style</label>
<select name="codeBlockStyle" id="codeBlockStyle">
<option value="indented">indented</option>
<option value="fenced">fenced</option>
</select>
</div>

<div class="form-group">
<label for="fence">Fence</label>
<select name="fence" id="fence">
<option value="```">```</option>
<option value="~~~">~~~</option>
</select>
</div>

<div class="form-group">
<label for="emDelimiter">Em delimiter</label>
<select name="emDelimiter" id="emDelimiter">
<option value="_">_</option>
<option value="*">*</option>
</select>
</div>

<div class="form-group">
<label for="strongDelimiter">Strong delimiter</label>
<select name="strongDelimiter" id="strongDelimiter">
<option value="**">**</option>
<option value="__">__</option>
</select>
</div>

<div class="form-group">
<label for="linkStyle">Link style</label>
<select name="linkStyle" id="linkStyle">
<option value="inlined">inlined</option>
<option value="referenced">referenced</option>
</select>
</div>

<div class="form-group">
<label for="linkReferenceStyle">Link reference style</label>
<select name="linkReferenceStyle" id="linkReferenceStyle">
<option value="full">full</option>
<option value="collapsed">collapsed</option>
<option value="shortcut">shortcut</option>
</select>
</div>
</form>
</div>

<footer><p>Turndown is copyright © 2017 <a href="http://www.domchristie.co.uk/">Dom Christie</a> and is released under the MIT license</p></footer>
<script>
;(function () {
var input = document.getElementById('input')
var output = document.getElementById('output')
var optionsForm = document.getElementById('options')
var turndownService = new window.TurndownService(options())
TurndownPluginGfmService.gfm(turndownService)

input.addEventListener('input', update)

optionsForm.addEventListener('change', function () {
turndownService = new window.TurndownService(options())
TurndownPluginGfmService.gfm(turndownService)
update()
})

update()

function update () {
output.value = turndownService.turndown(input.value)
}

function options () {
var opts = {}
var inputs = optionsForm.getElementsByTagName('select')
for (var i = 0; i < inputs.length; i++) {
var input = inputs[i]
opts[input.name] = input.value
}
return opts
}
})()
</script>
</body>
</html>
Loading