Skip to content

Commit

Permalink
Allow extra spaces and linebreaks in DFN autolinking
Browse files Browse the repository at this point in the history
Fixes #93.
  • Loading branch information
bterlson committed May 9, 2016
1 parent 36784ac commit fba8f12
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
27 changes: 22 additions & 5 deletions lib/autolinker.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ function linkClause(spec, clause, replacerCache) {
autolinkmap = {};

spec.biblio.inScopeByType(clause.namespace, 'term')
.forEach(entry => autolinkmap[entry.key.toLowerCase()] = entry);
.forEach(entry => autolinkmap[narrowSpace(entry.key.toLowerCase())] = entry);

spec.biblio.inScopeByType(clause.namespace, 'op')
.forEach(entry => autolinkmap[entry.key.toLowerCase()] = entry);
.forEach(entry => autolinkmap[narrowSpace(entry.key.toLowerCase())] = entry);

clauseReplacer = new RegExp(Object.keys(autolinkmap)
.sort(function (a, b) { return b.length - a.length; })
Expand All @@ -48,9 +48,13 @@ function linkClause(spec, clause, replacerCache) {

if (entry.type === 'term') {
if (isCommonTerm(key)) {
return '\\b' + key + '\\b(?!\\.\\w|%%|\\]\\])';
return '\\b' +
widenSpace(key) +
'\\b(?!\\.\\w|%%|\\]\\])';
} else if (key[0].match(/[A-Za-z0-9]/)) {
return '\\b' + caseInsensitiveRegExp(key) + '\\b(?!\\.\\w|%%|\\]\\])';
return '\\b' +
widenSpace(caseInsensitiveRegExp(key)) +
'\\b(?!\\.\\w|%%|\\]\\])';
} else {
return key;
}
Expand Down Expand Up @@ -86,7 +90,7 @@ function autolink(replacer, autolinkmap, spec, node, parentId, allowSameId) {
const template = spec.doc.createElement('template');
const content = escape(node.textContent);
const autolinked = content.replace(replacer, match => {
const entry = autolinkmap[match.toLowerCase()];
const entry = autolinkmap[narrowSpace(match.toLowerCase())];
if (!entry) {
return match;
}
Expand Down Expand Up @@ -132,3 +136,16 @@ function caseInsensitiveRegExp(str) {

return `[${lower}${upper}]${str.slice(1)}`;
}

// given a regexp string, returns a regexp string where each space can be
// many spaces or line breaks.
function widenSpace(str) {
return str.replace(/\s+/g, '[\\s\\r\\n]+');
}

// given a regexp string, returns a regexp string where multiple spaces
// or linebreaks can only be a single space
function narrowSpace(str) {
return str.replace(/[\s\r\n]+/g, ' ');
}

5 changes: 4 additions & 1 deletion test/autolinking.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ <h1>Autolinking</h1>
<p><dfn>Lowercase</dfn></p>
<p><dfn>strict mode</dfn></p>
<p><dfn>%Percent%</dfn></p>
<p><dfn>extra spaces</dfn></p>
</emu-clause>
<emu-clause id="sec-bar">
<h1>Autolinking 2</h1>
<p>lowercase should not autolink. But Lowercase should. But not LowerCase.</p>
<p>Strict mode shoud link. But Strict Mode should not.</p>
<p>Strict mode shoud link. But Strict Mode should not. Also, strict
mode can be wrapped across lines and contain extra whitespace.</p>
<p>extra spaces in a dfn should be narrowed to one space.</p>
<p>%Percent% should autolink.</p>
<p>Vars to dfns should be vars not dfns: _Lowercase_.</p>
<p>Also, no autolinks in <a href="#">anchors: Lowercase</a>.</p>
Expand Down
5 changes: 4 additions & 1 deletion test/autolinking.html.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
<p><dfn>Lowercase</dfn></p>
<p><dfn>strict mode</dfn></p>
<p><dfn>%Percent%</dfn></p>
<p><dfn>extra spaces</dfn></p>
</emu-clause>
<emu-clause id="sec-bar">
<h1><span class="secnum">2</span>Autolinking 2<span class="utils"><span class="anchor"><a href="#sec-bar">#</a></span></span></h1>
<p>lowercase should not autolink. But <emu-xref href="#sec-foo"><a href="#sec-foo">Lowercase</a></emu-xref> should. But not LowerCase.</p>
<p><emu-xref href="#sec-foo"><a href="#sec-foo">Strict mode</a></emu-xref> shoud link. But Strict Mode should not.</p>
<p><emu-xref href="#sec-foo"><a href="#sec-foo">Strict mode</a></emu-xref> shoud link. But Strict Mode should not. Also, <emu-xref href="#sec-foo"><a href="#sec-foo">strict
mode</a></emu-xref> can be wrapped across lines and contain extra whitespace.</p>
<p><emu-xref href="#sec-foo"><a href="#sec-foo">extra spaces</a></emu-xref> in a dfn should be narrowed to one space.</p>
<p><emu-xref href="#sec-foo"><a href="#sec-foo">%Percent%</a></emu-xref> should autolink.</p>
<p>Vars to dfns should be vars not dfns: <var>Lowercase</var>.</p>
<p>Also, no autolinks in <a href="#">anchors: Lowercase</a>.</p>
Expand Down

0 comments on commit fba8f12

Please sign in to comment.