slackify-markdown
Advanced tools
Comparing version 4.0.0 to 4.1.0
{ | ||
"name": "slackify-markdown", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"description": "Convert markdown into Slack-specific markdown", | ||
@@ -41,3 +41,5 @@ "license": "MIT", | ||
"remark-stringify": "^9.0.1", | ||
"unified": "^9.0.0" | ||
"unified": "^9.0.0", | ||
"unist-util-remove": "^2.0.1", | ||
"unist-util-visit": "^2.0.3" | ||
}, | ||
@@ -44,0 +46,0 @@ "devDependencies": { |
@@ -5,11 +5,21 @@ const gfm = require('remark-gfm'); | ||
const unified = require('unified'); | ||
const slackifyOptions = require('./slackify'); | ||
module.exports = (markdown, options) => unified() | ||
.use(parse, options) | ||
// 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(); | ||
const { collectDefinitions, removeDefinitions } = require('./definitions'); | ||
const createSlackifyOptions = require('./slackify'); | ||
module.exports = (markdown, options) => { | ||
const definitions = {}; | ||
const slackifyOptions = createSlackifyOptions(definitions); | ||
return unified() | ||
.use(parse, options) | ||
// Delete node is defined in GFM | ||
// https://github.com/syntax-tree/mdast/blob/main/readme.md#gfm | ||
.use(gfm) | ||
.use(collectDefinitions, definitions) | ||
.use(removeDefinitions) | ||
.use(stringify, slackifyOptions) | ||
.processSync(markdown) | ||
.toString(); | ||
}; |
@@ -10,5 +10,11 @@ const defaultHandlers = require('mdast-util-to-markdown/lib/handle'); | ||
/** | ||
* @type import('mdast-util-to-markdown').Handlers | ||
* Creates custom `mdast-util-to-markdown` handlers that tailor the output for | ||
* Slack Markdown. | ||
* | ||
* @param {Readonly<Record<string, { title: null | string, url: string }>>} definitions | ||
* Record of `Definition`s in the Markdown document, keyed by identifier. | ||
* | ||
* @returns {import('mdast-util-to-markdown').Handlers} | ||
*/ | ||
const handlers = { | ||
const createHandlers = definitions => ({ | ||
heading: (node, _parent, context) => { | ||
@@ -70,8 +76,8 @@ // make headers to be just *strong* | ||
const exit = context.enter('link'); | ||
const text = node.title | ||
|| phrasing(node, context, { before: '|', after: '>' }); | ||
const text = phrasing(node, context, { before: '|', after: '>' }) | ||
|| node.title; | ||
const url = encodeURI(node.url); | ||
exit(); | ||
if (!isURL(url)) return url; | ||
if (!isURL(url)) return text || url; | ||
@@ -81,9 +87,21 @@ return text ? `<${url}|${text}>` : `<${url}>`; | ||
linkReference: (node, _parent, context) => { | ||
const exit = context.enter('linkReference'); | ||
const definition = definitions[node.identifier]; | ||
const text = phrasing(node, context, { before: '|', after: '>' }) | ||
|| (definition ? definition.title : null); | ||
exit(); | ||
if (!definition || !isURL(definition.url)) return text; | ||
return text ? `<${definition.url}|${text}>` : `<${definition.url}>`; | ||
}, | ||
image: (node, _parent, context) => { | ||
const exit = context.enter('image'); | ||
const text = node.title || node.alt; | ||
const text = node.alt || node.title; | ||
const url = encodeURI(node.url); | ||
exit(); | ||
if (!isURL(url)) return url; | ||
if (!isURL(url)) return text || url; | ||
@@ -93,2 +111,14 @@ return text ? `<${url}|${text}>` : `<${url}>`; | ||
imageReference: (node, _parent, context) => { | ||
const exit = context.enter('imageReference'); | ||
const definition = definitions[node.identifier]; | ||
const text = node.alt | ||
|| (definition ? definition.title : null); | ||
exit(); | ||
if (!definition || !isURL(definition.url)) return text; | ||
return text ? `<${definition.url}|${text}>` : `<${definition.url}>`; | ||
}, | ||
text: (node, _parent, context) => { | ||
@@ -108,12 +138,18 @@ const exit = context.enter('text'); | ||
}, | ||
}; | ||
}); | ||
/** | ||
* @type import('remark-stringify').RemarkStringifyOptions | ||
* Creates options to be passed into a `remark-stringify` processor that tailor | ||
* the output for Slack Markdown. | ||
* | ||
* @param {Readonly<Record<string, { title: null | string, url: string }>>} definitions | ||
* Record of `Definition`s in the Markdown document, keyed by identifier. | ||
* | ||
* @returns {import('remark-stringify').RemarkStringifyOptions} | ||
*/ | ||
const options = { | ||
const createOptions = definitions => ({ | ||
bullet: '*', | ||
handlers, | ||
}; | ||
handlers: createHandlers(definitions), | ||
}); | ||
module.exports = options; | ||
module.exports = createOptions; |
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
10141
9
186
7
+ Addedunist-util-remove@^2.0.1
+ Addedunist-util-visit@^2.0.3
+ Addedunist-util-remove@2.1.0(transitive)
+ Addedunist-util-visit@2.0.3(transitive)