telegramify-markdown
Advanced tools
Comparing version 1.0.7 to 1.1.0
@@ -10,9 +10,9 @@ const gfm = require('remark-gfm'); | ||
module.exports = (markdown, options) => { | ||
module.exports = (markdown, unsupportedTagsStrategy) => { | ||
const definitions = {}; | ||
const telegramifyOptions = createTelegramifyOptions(definitions); | ||
const telegramifyOptions = createTelegramifyOptions(definitions, unsupportedTagsStrategy); | ||
return unified() | ||
.use(parse, options) | ||
.use(parse) | ||
.use(gfm) | ||
@@ -19,0 +19,0 @@ .use(removeComments) |
const defaultHandlers = require('mdast-util-to-markdown/lib/handle'); | ||
const phrasing = require('mdast-util-to-markdown/lib/util/container-phrasing'); | ||
const {wrap, isURL, escapeSymbols} = require('./utils'); | ||
const {wrap, isURL, escapeSymbols, processUnsupportedTags} = require('./utils'); | ||
@@ -15,3 +15,3 @@ /** | ||
*/ | ||
const createHandlers = definitions => ({ | ||
const createHandlers = (definitions, unsupportedTagsStrategy) => ({ | ||
heading: (node, _parent, context) => { | ||
@@ -58,4 +58,3 @@ // make headers to be just *strong* | ||
list: (...args) => defaultHandlers.list(...args).replace(/^(\d+)./gm, '$1\\.') | ||
, | ||
list: (...args) => defaultHandlers.list(...args).replace(/^(\d+)./gm, '$1\\.'), | ||
@@ -132,2 +131,7 @@ listItem: (...args) => defaultHandlers.listItem(...args).replace(/^\*/, '•'), | ||
}, | ||
blockquote: (node, _parent, context) => | ||
processUnsupportedTags(defaultHandlers.blockquote(node, _parent, context), unsupportedTagsStrategy), | ||
html: (node, _parent, context) => | ||
processUnsupportedTags(defaultHandlers.html(node, _parent, context), unsupportedTagsStrategy), | ||
}); | ||
@@ -144,7 +148,8 @@ | ||
*/ | ||
const createOptions = definitions => ({ | ||
const createOptions = (definitions, unsupportedTagsStrategy) => ({ | ||
bullet: '*', | ||
handlers: createHandlers(definitions), | ||
tightDefinitions: true, | ||
handlers: createHandlers(definitions, unsupportedTagsStrategy), | ||
}); | ||
module.exports = createOptions; |
117
lib/utils.js
const { URL } = require('url'); | ||
module.exports = { | ||
wrap(string, ...wrappers) { | ||
return [ | ||
...wrappers, | ||
string, | ||
...wrappers.reverse(), | ||
].join(''); | ||
}, | ||
function wrap(string, ...wrappers) { | ||
return [ | ||
...wrappers, | ||
string, | ||
...wrappers.reverse(), | ||
].join(''); | ||
} | ||
isURL(string) { | ||
try { | ||
return Boolean(new URL(string)); | ||
} catch (error) { | ||
return false; | ||
} | ||
}, | ||
function isURL(string) { | ||
try { | ||
return Boolean(new URL(string)); | ||
} catch (error) { | ||
return false; | ||
} | ||
} | ||
escapeSymbols(text, textType = 'text') { | ||
if (!text) { | ||
return text; | ||
} | ||
switch (textType) { | ||
case 'code': | ||
return text | ||
.replace(/`/g, '\\`') | ||
.replace(/\\/g, '\\\\') | ||
case 'link': | ||
return text | ||
.replace(/\(/g, '\\(') | ||
.replace(/\)/g, '\\)') | ||
.replace(/\\/g, '\\\\') | ||
default: | ||
return text | ||
.replace(/_/g, '\\_') | ||
.replace(/\*/g, '\\*') | ||
.replace(/\[/g, '\\[') | ||
.replace(/]/g, '\\]') | ||
.replace(/\(/g, '\\(') | ||
.replace(/\)/g, '\\)') | ||
.replace(/~/g, '\\~') | ||
.replace(/`/g, '\\`') | ||
.replace(/>/g, '\\>') | ||
.replace(/#/g, '\\#') | ||
.replace(/\+/g, '\\+') | ||
.replace(/-/g, '\\-') | ||
.replace(/=/g, '\\=') | ||
.replace(/\|/g, '\\|') | ||
.replace(/{/g, '\\{') | ||
.replace(/}/g, '\\}') | ||
.replace(/\./g, '\\.') | ||
.replace(/!/g, '\\!'); | ||
function escapeSymbols(text, textType = 'text') { | ||
if (!text) { | ||
return text; | ||
} | ||
switch (textType) { | ||
case 'code': | ||
return text | ||
.replace(/`/g, '\\`') | ||
.replace(/\\/g, '\\\\') | ||
case 'link': | ||
return text | ||
.replace(/\(/g, '\\(') | ||
.replace(/\)/g, '\\)') | ||
.replace(/\\/g, '\\\\') | ||
default: | ||
return text | ||
.replace(/_/g, '\\_') | ||
.replace(/\*/g, '\\*') | ||
.replace(/\[/g, '\\[') | ||
.replace(/]/g, '\\]') | ||
.replace(/\(/g, '\\(') | ||
.replace(/\)/g, '\\)') | ||
.replace(/~/g, '\\~') | ||
.replace(/`/g, '\\`') | ||
.replace(/>/g, '\\>') | ||
.replace(/#/g, '\\#') | ||
.replace(/\+/g, '\\+') | ||
.replace(/-/g, '\\-') | ||
.replace(/=/g, '\\=') | ||
.replace(/\|/g, '\\|') | ||
.replace(/{/g, '\\{') | ||
.replace(/}/g, '\\}') | ||
.replace(/\./g, '\\.') | ||
.replace(/!/g, '\\!'); | ||
} | ||
} | ||
} | ||
function processUnsupportedTags(content, strategy) { | ||
switch (strategy) { | ||
case 'escape': | ||
return escapeSymbols(content); | ||
case 'remove': | ||
return ''; | ||
case 'keep': | ||
default: | ||
return content; | ||
} | ||
} | ||
module.exports = { | ||
wrap, | ||
isURL, | ||
escapeSymbols, | ||
processUnsupportedTags, | ||
}; |
{ | ||
"name": "telegramify-markdown", | ||
"version": "1.0.7", | ||
"version": "1.1.0", | ||
"description": "Convert markdown into Telegram-specific markdown", | ||
@@ -51,3 +51,3 @@ "main": "index.js", | ||
"@commitlint/config-conventional": "^12.1.1", | ||
"codecov": "^3.8.2", | ||
"codecov": "^3.8.3", | ||
"eslint": "^7.24.0", | ||
@@ -54,0 +54,0 @@ "husky": "^6.0.0", |
@@ -49,2 +49,37 @@ # Telegramify-Markdown | ||
## Possible options | ||
You can also add unsupported tags strategy as a second argument, which can be one of the following: | ||
- `escape` - escape unsupported symbols for unsupported tags | ||
- `remove` - remove unsupported tags | ||
- `keep` - ignore unsupported tags (default) | ||
```js | ||
const telegramifyMarkdown = require('telegramify-markdown'); | ||
const markdown = ` | ||
# Header | ||
> Blockquote | ||
<div>Text in div</div> | ||
`; | ||
telegramifyMarkdown(markdown, 'escape'); | ||
/* | ||
*Header* | ||
\> Blockquote | ||
<div\>Text in div</div\> | ||
*/ | ||
telegramifyMarkdown(markdown, 'remove'); | ||
/* | ||
*Header* | ||
*/ | ||
``` | ||
sec | ||
[MIT Licence](LICENSE) |
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
12160
234
85