slackify-markdown
Advanced tools
Comparing version 3.1.0 to 4.0.0
declare module "slackify-markdown" { | ||
function slackify(markdown: string): string; | ||
function slackify(markdown: string, parseOption?: any): string; | ||
export = slackify; | ||
} |
{ | ||
"name": "slackify-markdown", | ||
"version": "3.1.0", | ||
"version": "4.0.0", | ||
"description": "Convert markdown into Slack-specific markdown", | ||
@@ -37,4 +37,6 @@ "license": "MIT", | ||
"dependencies": { | ||
"remark-parse": "^8.0.2", | ||
"remark-stringify": "^8.0.0", | ||
"mdast-util-to-markdown": "^0.6.2", | ||
"remark-gfm": "^1.0.0", | ||
"remark-parse": "^9.0.0", | ||
"remark-stringify": "^9.0.1", | ||
"unified": "^9.0.0" | ||
@@ -41,0 +43,0 @@ }, |
# Slackify-Markdown | ||
[![Build Status](https://travis-ci.org/jsarafajr/slackify-markdown.svg?branch=master)](https://travis-ci.org/jsarafajr/slackify-markdown) | ||
![Build Status](https://github.com/jsarafajr/slackify-markdown/workflows/Build%20CI/badge.svg?branch=master) | ||
[![codecov](https://codecov.io/gh/jsarafajr/slackify-markdown/branch/master/graph/badge.svg)](https://codecov.io/gh/jsarafajr/slackify-markdown) [![Known Vulnerabilities](https://snyk.io/test/github/jsarafajr/slackify-markdown/badge.svg)](https://snyk.io/test/github/jsarafajr/slackify-markdown) | ||
@@ -49,6 +49,3 @@ | ||
### Copyright and License | ||
Copyright Yevhenii Baraniuk, 2019 | ||
[MIT Licence](LICENSE) |
@@ -0,9 +1,14 @@ | ||
const gfm = require('remark-gfm'); | ||
const parse = require('remark-parse'); | ||
const stringify = require('remark-stringify'); | ||
const unified = require('unified'); | ||
const parse = require('remark-parse'); | ||
const slackify = require('./slackify'); | ||
const slackifyOptions = require('./slackify'); | ||
module.exports = (markdown, options) => unified() | ||
.use(parse, options) | ||
.use(slackify) | ||
// Delete node is defined in GFM | ||
// https://github.com/syntax-tree/mdast/blob/main/readme.md#gfm | ||
.use(gfm) | ||
.use(stringify, slackifyOptions) | ||
.processSync(markdown) | ||
.toString(); |
@@ -1,2 +0,4 @@ | ||
const { Compiler } = require('remark-stringify'); | ||
const defaultHandlers = require('mdast-util-to-markdown/lib/handle'); | ||
const phrasing = require('mdast-util-to-markdown/lib/util/container-phrasing'); | ||
const { wrap, isURL } = require('./utils'); | ||
@@ -7,75 +9,107 @@ | ||
const visitors = { | ||
heading(node) { | ||
/** | ||
* @type import('mdast-util-to-markdown').Handlers | ||
*/ | ||
const handlers = { | ||
heading: (node, _parent, context) => { | ||
// make headers to be just *strong* | ||
return wrap(this.content(node), '*'); | ||
}, | ||
const marker = '*'; | ||
strong(node) { | ||
return wrap(this.content(node), zeroWidthSpace, '*'); | ||
const exit = context.enter('heading'); | ||
const value = phrasing(node, context, { before: marker, after: marker }); | ||
exit(); | ||
return wrap(value, marker); | ||
}, | ||
delete(node) { | ||
return wrap(this.content(node), zeroWidthSpace, '~'); | ||
strong: (node, _parent, context) => { | ||
const marker = '*'; | ||
const exit = context.enter('strong'); | ||
const value = phrasing(node, context, { before: marker, after: marker }); | ||
exit(); | ||
return wrap(value, zeroWidthSpace, marker); | ||
}, | ||
emphasis(node) { | ||
return wrap(this.content(node), zeroWidthSpace, '_'); | ||
delete(node, _parent, context) { | ||
const marker = '~'; | ||
const exit = context.enter('delete'); | ||
const value = phrasing(node, context, { before: marker, after: marker }); | ||
exit(); | ||
return wrap(value, zeroWidthSpace, marker); | ||
}, | ||
list(node) { | ||
const listItem = this.visitors.listItem.bind(this); | ||
emphasis: (node, _parent, context) => { | ||
const marker = '_'; | ||
return node.children.map((child, index) => { | ||
const bullet = node.ordered | ||
? `${node.start + index}.` | ||
: '•'; | ||
return listItem(child, node, index, bullet); | ||
}).join('\n'); | ||
const exit = context.enter('emphasis'); | ||
const value = phrasing(node, context, { before: marker, after: marker }); | ||
exit(); | ||
return wrap(value, zeroWidthSpace, marker); | ||
}, | ||
code(node) { | ||
listItem: (...args) => defaultHandlers | ||
.listItem(...args) | ||
.replace(/^\*/, '•'), | ||
code(node, _parent, context) { | ||
const exit = context.enter('code'); | ||
// delete language prefix for deprecated markdown formatters (old Bitbucket Editor) | ||
const content = node.value.replace(/^#![a-z]+\n/, ''); // ```\n#!javascript\ncode block\n``` | ||
exit(); | ||
return wrap(content, '```', '\n'); | ||
}, | ||
link(node) { | ||
const text = node.title || this.content(node); | ||
return this.visitors.url.call(this, node, text); | ||
link: (node, _parent, context) => { | ||
const exit = context.enter('link'); | ||
const text = node.title | ||
|| phrasing(node, context, { before: '|', after: '>' }); | ||
const url = encodeURI(node.url); | ||
exit(); | ||
if (!isURL(url)) return url; | ||
return text ? `<${url}|${text}>` : `<${url}>`; | ||
}, | ||
image(node) { | ||
image: (node, _parent, context) => { | ||
const exit = context.enter('image'); | ||
const text = node.title || node.alt; | ||
return this.visitors.url.call(this, node, text); | ||
}, | ||
const url = encodeURI(node.url); | ||
exit(); | ||
url(node, text) { | ||
const url = this.encode(node.url || '', node); | ||
if (!isURL(url)) return url; | ||
return text ? `<${url}|${text}>` : `<${url}>`; | ||
}, | ||
}; | ||
class SlackCompiler extends Compiler { | ||
constructor(...args) { | ||
super(...args); | ||
this.visitors = Object.assign(this.visitors, visitors); | ||
this.escape = this.slackEscape.bind(this); | ||
} | ||
slackEscape(value, node, parent) { | ||
return value | ||
text: (node, _parent, context) => { | ||
const exit = context.enter('text'); | ||
// https://api.slack.com/reference/surfaces/formatting#escaping | ||
const text = node.value | ||
.replace(/&/g, '&') | ||
.replace(/</g, '<') | ||
.replace(/>/g, '>'); | ||
} | ||
exit(); | ||
content(node) { | ||
return this.all(node).join(''); | ||
} | ||
} | ||
// Do we need more escaping like the default handler uses? | ||
// https://github.com/syntax-tree/mdast-util-to-markdown/blob/main/lib/handle/text.js | ||
// https://github.com/syntax-tree/mdast-util-to-markdown/blob/main/lib/unsafe.js | ||
return text; | ||
}, | ||
}; | ||
module.exports = function slackify() { | ||
this.Compiler = SlackCompiler; | ||
/** | ||
* @type import('remark-stringify').RemarkStringifyOptions | ||
*/ | ||
const options = { | ||
bullet: '*', | ||
handlers, | ||
}; | ||
module.exports = options; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7537
122
5
51
+ Addedremark-gfm@^1.0.0
+ Added@types/mdast@3.0.15(transitive)
+ Addeddebug@4.3.7(transitive)
+ Addedescape-string-regexp@4.0.0(transitive)
+ Addedmdast-util-find-and-replace@1.1.1(transitive)
+ Addedmdast-util-from-markdown@0.8.5(transitive)
+ Addedmdast-util-gfm@0.1.2(transitive)
+ Addedmdast-util-gfm-autolink-literal@0.1.3(transitive)
+ Addedmdast-util-gfm-strikethrough@0.2.3(transitive)
+ Addedmdast-util-gfm-table@0.1.6(transitive)
+ Addedmdast-util-gfm-task-list-item@0.1.6(transitive)
+ Addedmdast-util-to-markdown@0.6.5(transitive)
+ Addedmdast-util-to-string@2.0.0(transitive)
+ Addedmicromark@2.11.4(transitive)
+ Addedmicromark-extension-gfm@0.3.3(transitive)
+ Addedmicromark-extension-gfm-autolink-literal@0.5.7(transitive)
+ Addedmicromark-extension-gfm-strikethrough@0.6.5(transitive)
+ Addedmicromark-extension-gfm-table@0.4.3(transitive)
+ Addedmicromark-extension-gfm-tagfilter@0.3.0(transitive)
+ Addedmicromark-extension-gfm-task-list-item@0.3.3(transitive)
+ Addedms@2.1.3(transitive)
+ Addedremark-gfm@1.0.0(transitive)
+ Addedremark-parse@9.0.0(transitive)
+ Addedremark-stringify@9.0.1(transitive)
+ Addedzwitch@1.0.5(transitive)
- Removedcharacter-entities-html4@1.1.4(transitive)
- Removedcollapse-white-space@1.0.6(transitive)
- Removedinherits@2.0.4(transitive)
- Removedis-alphanumeric@1.0.0(transitive)
- Removedis-whitespace-character@1.0.4(transitive)
- Removedis-word-character@1.0.4(transitive)
- Removedmarkdown-escapes@1.0.4(transitive)
- Removedmdast-util-compact@2.0.1(transitive)
- Removedremark-parse@8.0.3(transitive)
- Removedremark-stringify@8.1.1(transitive)
- Removedstate-toggle@1.0.3(transitive)
- Removedstringify-entities@3.1.0(transitive)
- Removedtrim@0.0.1(transitive)
- Removedtrim-trailing-lines@1.1.4(transitive)
- Removedunherit@1.1.3(transitive)
- Removedunist-util-remove-position@2.0.1(transitive)
- Removedunist-util-visit@2.0.3(transitive)
- Removedvfile-location@3.2.0(transitive)
- Removedxtend@4.0.2(transitive)
Updatedremark-parse@^9.0.0
Updatedremark-stringify@^9.0.1