Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,22 @@ mrkdwn(html)
// }
```

You can customize the conversion with some options:

```js
const mrkdwn = require('html-to-mrkdwn')

const html = `
<p>Hello</p>
<hr>
<p>Goodbye</p>
`

mrkdwn(html, {trimAt: 'hr'})
// {
// text: "Hello",
// }

```

[![Greenkeeper badge](https://badges.greenkeeper.io/github-slack/html-to-mrkdwn.svg)](https://greenkeeper.io/)
7 changes: 1 addition & 6 deletions lib/first-image.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
const {JSDOM} = require('jsdom')

module.exports = html => {
// Parse the HTML
const frag = JSDOM.fragment(html)

module.exports = frag => {
const img = frag.querySelector('img')

if (img) {
Expand Down
9 changes: 6 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const TurnDown = require('turndown')
const { strikethrough, taskListItems } = require('turndown-plugin-gfm')

const parseHtml = require('./parse-html')
const firstImage = require('./first-image')
const rules = require('./rules')

Expand All @@ -27,9 +28,11 @@ service.remove('table')

Object.keys(rules).forEach(rule => service.addRule(rule, rules[rule]))

module.exports = (html) => {
module.exports = (html, options) => {
options = options || {}
const frag = parseHtml(html, {trimAt: options.trimAt})
return {
text: service.turndown(html),
image: firstImage(html)
text: service.turndown(frag),
image: firstImage(frag)
}
}
23 changes: 23 additions & 0 deletions lib/parse-html.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const {JSDOM} = require('jsdom')

module.exports = (html, options) => {
const frag = JSDOM.fragment(html)
if (options.trimAt) {
const node = frag.querySelector(options.trimAt)
if (node) {
removeAllStartingAt(node)
}
}
return frag
}

function removeAllStartingAt (node) {
while (node) {
let nextNode = node.nextSibling
if (!nextNode && node.parentNode) {
nextNode = node.parentNode.nextSibling
}
node.parentNode.removeChild(node)
node = nextNode
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "html-to-mrkdwn",
"version": "3.0.0",
"version": "3.1.0",
"description": "Convert HTML to Slack flavored mrkdwn",
"main": "./lib",
"scripts": {
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/hr-trim-nested.mrkdwn
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<p>
Before
<hr>
After
</p>
<p>Really after</p>
==== {"trimAt": "hr"}
Before
5 changes: 5 additions & 0 deletions test/fixtures/hr-trim.mrkdwn
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<p>Before</p>
<hr>
<p>After</p>
==== {"trimAt": "hr"}
Before
9 changes: 9 additions & 0 deletions test/fixtures/hr.mrkdwn
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<p>Before</p>
<hr>
<p>After</p>
====
Before

* * *

After
5 changes: 3 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ fs.readdirSync('test/fixtures').forEach(file => {

test(file.replace('.mrkdwn', ''), () => {
const content = fs.readFileSync(`test/fixtures/${file}`).toString()
const [input, output] = content.split('====')
expect(mrkdwn(input).text.trim()).toEqual(output.trim())
const [input, options, output] = content.split(/^====(.*)$/m)
const optionsObject = options ? JSON.parse(options) : undefined
expect(mrkdwn(input, optionsObject).text.trim()).toEqual(output.trim())
})
})

Expand Down