Skip to content

Commit f3b6b08

Browse files
committed
fix(config): regex improvements for links
1 parent 76c9533 commit f3b6b08

1 file changed

Lines changed: 24 additions & 14 deletions

File tree

eleventy.config.tracToHTML.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,36 +27,46 @@ module.exports = function tracToHTML(text) {
2727
)
2828
return `<pre class="wiki"></pre>`
2929
})
30+
// Linkify http links outside brackets
31+
.replace(/(^|\s)(https?:\/\/[^\s]+)/g, function (_match, space, url) {
32+
return `${
33+
space || ''
34+
}<a href="${url}" class="ext-link"><span class="icon"></span>${url}</a>`
35+
})
3036
// Linkify http links in brackets
3137
.replace(
32-
/\[(https?:\/\/[^\s\]]+)(?:\s+([^\]]+))?\]/g,
33-
function (_match, url, text) {
34-
return `<a href="${url}" class="ext-link"><span class="icon"></span>${
38+
/(^|\s)\[(https?:\/\/[^\s\]]+)(?:\s+([^\]]+))?\]/g,
39+
function (_match, space, url, text) {
40+
return `${
41+
space || ''
42+
}<a href="${url}" class="ext-link"><span class="icon"></span>${
3543
text || url
3644
}</a>`
3745
}
3846
)
3947
// Linkify hash links in brackets
4048
.replace(
41-
/\[(#[^\s\]]+)(?:\s+([^\]]+))?\]/g,
42-
function (_match, url, text) {
43-
return `<a href="${url}" class="ext-link"><span class="icon"></span>${
49+
/(^|\s)\[(#[^\s\]]+)(?:\s+([^\]]+))?\]/g,
50+
function (_match, space, url, text) {
51+
return `${
52+
space || ''
53+
}<a href="${url}" class="ext-link"><span class="icon"></span>${
4454
text || url
4555
}</a>`
4656
}
4757
)
4858
// Linkify CamelCase links in brackets
4959
.replace(
50-
/\[([A-Z][a-z]+[A-Z][\w#-]+)(?:\s+([^\]]+))?\]/g,
51-
function (_match, page, text) {
52-
return `<a href="/wiki/${page}">${text || page}</a>`
60+
/(^|\s)\[([A-Z][a-z]+[A-Z][\w#-]+)(?:\s+([^\]]+))?\]/g,
61+
function (_match, space, page, text) {
62+
return `${space || ''}<a href="/wiki/${page}">${text || page}</a>`
5363
}
5464
)
5565
// Linkify trac links
5666
.replace(
57-
/(?:\[trac:([^ ]+) "([^"]+)"\])|(?:\[trac:([^\s\]]+)(?: ([^\]]+))?\])/g,
58-
function (_match, quotepage, quotedtext, page, text) {
59-
return `<a href="https://trac.edgewall.org/intertrac/${
67+
/(^|\s)(?:\[trac:([^ ]+) "([^"]+)"\])|(?:\[trac:([^\s\]]+)(?: ([^\]]+))?\])/g,
68+
function (_match, space, quotepage, quotedtext, page, text) {
69+
return `${space || ''}<a href="https://trac.edgewall.org/intertrac/${
6070
quotepage || page
6171
}" class="ext-link"><span class="icon"></span>${
6272
quotedtext || text || page
@@ -70,9 +80,9 @@ module.exports = function tracToHTML(text) {
7080
/(^|\s)(!)?([A-Z][a-z]+[A-Z]\w+(?:#\w+)?)(?!\w)/g,
7181
function (_match, space, excl, page) {
7282
if (excl) {
73-
return `${space}${page}`
83+
return `${space || ''}${page}`
7484
}
75-
return `${space}<a href="/wiki/${page}">${page}</a>`
85+
return `${space || ''}<a href="/wiki/${page}">${page}</a>`
7686
}
7787
)
7888
// Convert ---- to <hr>

0 commit comments

Comments
 (0)