-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathauto-link-url.js
More file actions
26 lines (24 loc) · 937 Bytes
/
auto-link-url.js
File metadata and controls
26 lines (24 loc) · 937 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Re-adds support for <https://...> autolinks in MDX.
// remark-mdx treats angle brackets as JSX, so <https://google.com>
// fails to parse. This plugin converts autolinks to standard markdown
// link syntax [url](url) before the parser sees them.
//
// Replaces the old this.Parser.prototype.inlineTokenizers approach
// (removed in remark-parse v11). Compatible with unified v9 and v11.
function plugin() {
const self = this;
const originalParse = self.parse;
self.parse = function (file) {
const str = String(file.contents || file.value || '');
// Match <http://...> or <https://...> autolinks and convert to [url](url)
const converted = str.replace(/<(https?:\/\/[^>]+)>/g, '[$1]($1)');
if (file.contents !== undefined) {
file.contents = converted;
}
if (file.value !== undefined) {
file.value = converted;
}
return originalParse.call(this, file);
};
}
module.exports = plugin;