Installations
npm install gfm-escape
Developer
orchitech
Developer Guide
Module System
CommonJS, UMD
Min. Node Version
Typescript Support
No
Node Version
14.4.0
NPM Version
6.14.4
Statistics
6 Stars
33 Commits
1 Forks
4 Watching
10 Branches
2 Contributors
Updated on 21 Apr 2024
Languages
JavaScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
1,744,608
Last day
74.2%
6,564
Compared to previous day
Last week
5.3%
32,684
Compared to previous week
Last month
0.2%
134,455
Compared to previous month
Last year
889.6%
1,579,467
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
GfmEscape
...the only escaper passing backtranslation tests.
GfmEscape
is an enterprise-grade library for transforming untagged plain text
to CommonMark and
GitHub Flavored Markdown (GFM).
Why GfmEscape?
There are neat and configurable markup converters like Turndown, which even allows transforming any markup that can be converted to HTML first.
While conversion of inline and block constructs is well covered, little attention is paid to transforming text content itself. And this is tricky especially with non-delimited "extended" autolinks, which make escaping heavily context-dependent.
In short:
- No escaping breaks your output.
- Naive or aggressive escaping breaks your output.
- Overescaping would also break the John Gruber's overriding design goal for Markdown’s formatting syntax, i.e. to make it as readable as possible and publishable as-is, as plain text, without looking like it’s been marked up with tags or formatting instructions.
GfmEscape addresses these issues without significant performance penalty, as it is based on UnionReplacer. See below for more details.
Outline
Installation and usage
In browsers:
1<script src="https://unpkg.com/union-replacer/dist/union-replacer.umd.js"></script> 2<script src="https://unpkg.com/gfm-escape/dist/gfm-escape.umd.js"></script>
Using npm:
1npm install gfm-escape
In Node.js:
1const GfmEscape = require('gfm-escape');
Synopsis
escaper = new GfmEscape(escapingOptions[, syntax])
newStr = escaper.escape(input[, gfmContext[, metadata]])
A created GfmEscape
instance is intended to be reused and shared in your code.
Parameters
escapingOptions
: option object defining how to perform escaping, its keys
correspond to individual replaces. When a replace option is set to any truthy
value, suboption defaults are applied and can be overriden by passed suboptions.
A single option object can be reused for instantiating escapers for
different syntaxes, some options would just render irrelevant.
The current full options are:
1{ 2 strikethrough: { // default false 3 optimizeForDoubleTilde: false, 4 }, 5 extAutolink: { // default false 6 breakUrl: false, 7 breakWww: false, 8 breaker: '<!-- -->', 9 allowedTransformations: [ 'entities', 'commonmark' ], 10 allowAddHttpScheme: false, 11 inImage: false, 12 }, 13 table: true, // default false 14 emphasisNonDelimiters: { // default true 15 maxIntrawordUnderscoreRun: undefined, 16 }, 17 linkTitle: { // default true 18 delimiters: [ '"', '\'', '()' ], 19 alwaysEscapeDelimiters: [], 20 }, 21}
See below for more details.
syntax
: suggests the syntax escaper is built for.
The predefined syntaxes are available as members of GfmEscape.Syntax
:
text
: normal text, the default.linkDestination
: text rendered[sometext](here)
.cmAutolink
: text rendered<here>
. Please note that a valid CommonMark must contain a URI scheme, which cannot be addressed by the escaper. When deciding if CommonMark autolink is an appropriate construct to use, we suggest to use theisEncodable(input)
andwouldBeUnaltered(input)
methods on theSyntax.cmAutolink
object.codeSpan
: text rendered`here`
.linkTitle
: text rendered[text](destination "here")
or[text](destination 'here')
or[text](destination (here))
.
input
: the string to escape. Please note that correct escaping is currently
only guaranteed when the input is trimmed and normalized in terms of whitespace.
The library does not perfos qrm whitespace normalizing on its own, as it is often
ensured by the source's origin, e.g. textContent
of a normalized HTML DOM.
Manual normalizing can be done with input.trim().replace(/[ \t\n\r]+/g, ' ')
.
If it is intended to keep the source somewhat organized in lines, the minimum
treatment to make escaping safe would be input.replace(/^[ \t\r\n]+|[ \t]+$/gm, '')
.
In such case, the caller has a responsibility to place the output correctly in
the generated document. I.e. to indent all the lines when the context requires
indenting.
gfmContext
: extra contextual information to be considered. The contexts have
no defaults, i.e. they are falsy by default. The following contexts are available:
1{ 2 inLink: true, // indicates suppressing nested links 3 inImage: true, // similar to inLink for ![this image text](img.png) 4 inTable: true, // indicates extra escaping of table contents 5}
When escaping, metadata
is extra input-output parameter that collects
metadata about the actual escaping. Currently metadata
are used for
codeSpan
syntax and linkTitle
syntax.
1const escaper = new GfmEscape({ table: true }, GfmEscape.Syntax.codeSpan); 2const x = {}; // not necessary as the surrounding delimiter is always '`' 3const context = { inTable: true }; 4const escaped = escaper.escape('`array|string`', context, x); 5console.log(`\`${escaped}\``); // `` `array\|string` `` 6console.log(`${x.extraBacktickString.length} backtickts and ${x.extraSpace.length} spaces added.`); 7// 1 backticks and 1 spaces added. 8 9const linkTitleEscaper = new GfmEscape({}, GfmEscape.Syntax.linkTitle); 10const x = {}; // needed as we let GfmEscape decide the surrounding delimiter 11let escaped = escaper.escape('cool "link \'title\'"', context, x); 12console.log(`${x.startDelimiter}${escaped}${x.endDelimiter}`); 13// (cool "link 'title'") 14 15escaped = escaper.escape('average link title', context, x); 16console.log(`${x.startDelimiter}${escaped}${x.endDelimiter}`); 17// "average link title" 18 19const rigidLinkTitleEscaper = new GfmEscape({ 20 linkTitle: { 21 delimiters: '"', 22 } 23}, GfmEscape.Syntax.linkTitle); 24// metadata not necessary, as the surronding delimiter will be always '"' 25escaped = escaper.escape('cool "link \'title\'"'); 26console.log(`"${escaped}"`); 27// "cool \"link 'title'\""
Escaping options: strikethrough
Defaults to false
, i.e. '~' is not special and it is not escaped.
Suboptions:
optimizeForDoubleTilde
: only eventual sequences of two tildes are escaped. Defaultfalse
.
Escaping options: extAutolink
Defaults to false
, i.e. autolinks are not detected and do not form special
case for escaping.
Suboptions:
breakUrl
: if a string capable of forming extended url autolink is encountered, it is broken to prevent that. E.g.https://orchi.tech
becomeshttps://<!-- -->orchi.tech
. Defaultfalse
.breakWww
: if a string capable of forming extended www autolink is encountered, it is broken to prevent that. E.g.www.orchi.tech
becomeswww.<!-- -->orchi.tech
. Defaultfalse
.breaker
: a sequence used to break extended autolinks, used both for breaking and terminating. Default<!-- -->
. Please note that some Markdown renderers like Redcarpet do not support HTML comments - tag sequences like<span></span>
or artificial<nolink>
can be used instead.allowedTransformations
: array of transformations that are allowed if an extended autolink-like string needs to be transformed to retain the expected target and text. The order indicates priority. Defaults to['entities', 'commonmark']
. Available transformations are:'keep'
: always the most preferred, no reason to set it explicitly.'entites'
: entity name references are used to escape trailing characters. E.g.*http://orchi.tech,*
becomes\*http://orchi.tech,*
.'commonmark'
: a CommonMark autolink is used to delimit the actual link part. E.g.*http://orchi.tech,*
becomes\*<http://orchi.tech>,\*
.'breakup'
: autolink-like string is broken, so that it is not interpreted as an autolink. E.g.*https://orchi.tech,*
becomes\*https://<!-- -->orchi.tech,\*
.'breakafter'
: autolink-like string is terminated after the actual link part. E.g.*https://orchi.tech,*
becomes\*https://orchi.tech<!-- -->,\*
. This transformation is the default fallback, no reason to set it explicitly.
allowAddHttpScheme
: addhttp://
scheme when a transformation needs it to work. E.g.*www.orchi.tech,*
would become\*<http://www.orchi.tech>,\*
with thecommonmark
transformation.inImage
: suggest if extended autolink treatment should be applied within image text. Although the CommonMark spec says links are interpreted and just the stripped plain text part renders to thealt
attribute, cmark-gfm actually does not do it for extended autolinks, so the default is false.
How to choose the options:
- Consider rendering details of the target Markdown flavor. Backtranslation test should pass on text. And if a link is produced, it should match the input.
- Consider user expectations. The users probably don't expect HTML comments all over their documents. They probably don't expect HTML entity references too, but see also the next point.
- Consider declared semantics. Transforming to CommonMark autolinks looks quite
well, but CommonMark autolinks form explicit link demarkation when the input
was not explicitly link-demarked.
'breakafter'
might be better option in some situations. - Last, but not least - consider the origin of your input. If you transform
HTML rendered from another markup language that supports autolinking too,
you may expect that an autolink-suppression mechanism was used if an
autolink-like string is encountered in plain text. Then it might be better
to break it too.
And if the original renderer supports url autolinks, but not www autolinks, it might be better to set only'breakUrl'
, as users may still expect www links to be autolinked in the plain text.
Escaping options: emphasisNonDelimiters
Defaults to true
, i.e. intraword emphasis delimiters are not escaped if it is safe
not to escape them. E.g. in My account is joe_average.
, the underscore stays
unescaped as joe_average
, not .joe\_average
Suboptions:
maxIntrawordUnderscoreRun
: if defined as a number, it sets the maximum length of intraword underscores to be kept as is. E.g. for1
and inputjoe_average or joe__average
, the output would bejoe_average or joe\_\_average
. This is helpful for some renderers like Redcarpet. Bothundefined
andfalse
mean no limit on unescaped intraword underscore run length. Defaults toundefined
.
Escaping options: table
Defaults to false
, i.e. table pipes are not escaped. If enabled, rendering of table
delimiter rows is suppressed by escaping its pipes and all pipes are escaped when in
table context.
Escaping options: linkTitle
Suboptions:
delimiters
: array of allowed delimiter to be chosen from or a single delimiter. Delimiters are"
,'
and()
. When more delimiters are allowed, GfmEscape picks the least interferring one. The picked delimiter is returned in metadata, as shown in the example above.alwaysEscapeDelimiters
: array of delimiters that are always escaped.
GFM escaping details
Terminology:
- cmAutolink - CommonMark autolink
- cmUriAutolink - CommonMark URI autolink
- cmEmailAutolink - CommonMark email autolink
- extAutolink - GFM extended autolink
- extWebAutolink - GFM extended url or www autolink
- extUrlAutolink - GFM extended url autolink
- extWwwAutolink - GFM extended www autolink
- extEmailAutolink - GFM extended email autolink
- extWebAutolink - GFM extended url or www autolink
Specs:
Reference implementations examined:
Implementation notes
Remarks on cmark-gfm library
While cmark-gfm is somewhat a reference implementation of GFM Spec, we have found a few interesting details...
cmark_gfm-001
: Contrary to the GFM spec stating All such recognized autolinks can only come at the beginning of a line, after whitespace, or any of the delimiting characters *, _, ~, and (, it seems this applies just to extended www autolinks in cmark-gfm. E.g..https://orchi.tech
is recognized as an autolink by this library. We follow this.cmark_gfm-002
: Contrary to the GFM spec, extended autolinks in cmark-gfm do not treat[\v\f]
as space, while CM autolinks do. We follow this.cmark_gfm-003
: cmark-gfm considers<
as valid for autolink detection and trims the resulting link afterwards. Sohttps://or_chi.tech.<
leads to autolinking ofhttps://or_chi.tech
, although this wouldn't form autolink without the trailing<
. We follow this, but non-explicit extended autolink transformations would break the autolink detection - which is probaly good. E.g. with the default settings,https://or_chi.tech.<
leads tohttps://or_chi.tech.<
(wouldn't be detected as extended autolink by cmark-gfm), whilehttps://or_chi.tech.<~
leads to<https://or_chi.tech>\<\~
(forced CM autolink).cmark_gfm-004
: GFM spec says If an autolink ends in a semicolon (;), we check to see if it appears to resemble an entity reference; if the preceding text is & followed by one or more alphanumeric characters. If so, it is excluded from the autolink... Alphabetic references cmark-gfmcmark_gfm-005
: Backslash escape in link destination, e.g.[foo](http://orchi.tech/foo\_bar)
does not prevent entity reference from interpreting in rendered HTML. We use entity encoding instead, i.e.&
. The same applies to link titles.
TODO
- Minification for browsers.
- Complete and polish implementation remarks.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 0/23 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 8 are checked with a SAST tool
Reason
20 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-w8qv-6jwh-64r5
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-ww39-953v-wcq6
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-6fc8-4gx4-v693
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
Score
1.7
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More