-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(convert): add
unsupportedTagsStrategy
option that allows to es…
…cape unsupported tags Closes #5
- Loading branch information
1 parent
f10c703
commit 4e78930
Showing
5 changed files
with
148 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,74 @@ | ||
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters