Skip to content

Commit 5c83466

Browse files
Include ESM support (#2)
1 parent 94cae2c commit 5c83466

17 files changed

+341
-193
lines changed

.gitattributes

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
# Remove files for archives generated using `git archive`.
88
.* export-ignore
99
appveyor.yml export-ignore
10+
fixup export-ignore
1011
package-lock.json export-ignore
1112
src export-ignore
1213
test export-ignore
1314
tsconfig.json export-ignore
14-
types/index.test-d.ts export-ignore
15+
tsconfig-cjs.json export-ignore
16+
tsconfig-esm.json export-ignore
17+
types/ export-ignore

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# markdown-it-external-link changelog
22

3+
## 1.1.0 - Sep 27, 2023
4+
5+
### Bug
6+
7+
- Include ESM support
8+
39
## 1.0.2 - Aug 25, 2023
410

511
### Bug

dist/cjs/index.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import markdownit from 'markdown-it';
2+
3+
export default function markdownitExternalLink (md: markdownit, options: configOptions): void;
4+
5+
export type configOptions = {
6+
hosts: [];
7+
rel: string;
8+
target: string;
9+
};
File renamed without changes.

dist/cjs/index.test-d.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
import markdownit from 'markdown-it';
4+
import markdownitExternalLink from './index';
5+
6+
const testString = `This is an [Example](http://example.com) link, [Google](https://google.com) link, [Facebook](https://facebook.com) link, [Test Example](http://test.example.com/) link, [Test2 Example](http://test2.example.com/) link and [Relative](/docs/concept/) link.`;
7+
8+
const md = new markdownit();
9+
const internalHosts = {
10+
'hosts': [
11+
'http://example.com',
12+
'http://test.example.com'
13+
]
14+
};
15+
16+
if (internalHosts) {
17+
md.use(markdownitExternalLink, internalHosts);
18+
md.render(testString);
19+
}

dist/cjs/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "commonjs"
3+
}

dist/esm/index.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import markdownit from 'markdown-it';
2+
3+
export default function markdownitExternalLink (md: markdownit, options: configOptions): void;
4+
5+
export type configOptions = {
6+
hosts: [];
7+
rel: string;
8+
target: string;
9+
};

dist/esm/index.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import detectExternalLink from 'detect-external-link';
2+
export default function markdownitExternalLink(md, userConfig) {
3+
const defaultConfig = {
4+
hosts: [],
5+
rel: 'noopener',
6+
target: '_blank',
7+
};
8+
const defaultOpenRender = md.renderer.rules.link_open || function (tokens, idx, options, env, self) {
9+
return self.renderToken(tokens, idx, options);
10+
};
11+
const finalConfig = Object.assign({}, defaultConfig, userConfig);
12+
md.renderer.rules.link_open = function (tokens, idx, options, env, self) {
13+
if (tokens[idx] && tokens[idx].attrs) {
14+
const linkIndex = tokens[idx].attrIndex('href');
15+
if (linkIndex in tokens[idx].attrs && tokens[idx].attrs[linkIndex][1]) {
16+
if (detectExternalLink(tokens[idx].attrs[linkIndex][1], finalConfig)) {
17+
const relIndex = tokens[idx].attrIndex('rel');
18+
const targetIndex = tokens[idx].attrIndex('target');
19+
if (targetIndex < 0) {
20+
tokens[idx].attrPush(['target', finalConfig.target]);
21+
}
22+
else {
23+
tokens[idx].attrs[targetIndex][1] = finalConfig.target;
24+
}
25+
if (relIndex < 0) {
26+
tokens[idx].attrPush(['rel', finalConfig.rel]);
27+
}
28+
else {
29+
tokens[idx].attrs[relIndex][1] = finalConfig.rel;
30+
}
31+
}
32+
}
33+
}
34+
return defaultOpenRender(tokens, idx, options, env, self);
35+
};
36+
}

dist/esm/index.test-d.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
import markdownit from 'markdown-it';
4+
import markdownitExternalLink from './index';
5+
6+
const testString = `This is an [Example](http://example.com) link, [Google](https://google.com) link, [Facebook](https://facebook.com) link, [Test Example](http://test.example.com/) link, [Test2 Example](http://test2.example.com/) link and [Relative](/docs/concept/) link.`;
7+
8+
const md = new markdownit();
9+
const internalHosts = {
10+
'hosts': [
11+
'http://example.com',
12+
'http://test.example.com'
13+
]
14+
};
15+
16+
if (internalHosts) {
17+
md.use(markdownitExternalLink, internalHosts);
18+
md.render(testString);
19+
}

dist/esm/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "module"
3+
}

0 commit comments

Comments
 (0)