Skip to content

Commit cefe20a

Browse files
vsesh3y3
authored andcommitted
fix: absolute links in included files
1 parent 86e833a commit cefe20a

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

src/transform/plugins/links/index.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {PAGE_LINK_REGEXP} from './constants';
66
import Token from 'markdown-it/lib/token';
77
import {Logger} from 'src/transform/log';
88
import {MarkdownItPluginCb, MarkdownItPluginOpts} from '../typings';
9-
import path, {parse, relative, resolve} from 'path';
9+
import path, {isAbsolute, parse, relative, resolve} from 'path';
1010
import {StateCore} from 'src/transform/typings';
1111

1212
function defaultTransformLink(href: string) {
@@ -113,6 +113,10 @@ function processLink(state: StateCore, tokens: Token[], idx: number, opts: ProcO
113113
return;
114114
}
115115

116+
if (isAbsolute(href)) {
117+
return;
118+
}
119+
116120
if (pathname) {
117121
file = resolve(path.parse(currentPath).dir, pathname);
118122
fileExists = isFileExists(file);

test/links.test.ts

+16
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,20 @@ describe('Links', () => {
7777

7878
expect(result).toEqual('<h1>First</h1>\n<p><a href="./second.html">Second</a></p>\n');
7979
});
80+
81+
test('Should create link with the absolute path', () => {
82+
const inputPath = resolve(__dirname, './mocks/absolute-link.md');
83+
const input = readFileSync(inputPath, 'utf8');
84+
const result = transformYfm(input, inputPath);
85+
86+
expect(result).toEqual('<p><a href="/link/">Absolute link</a></p>\n');
87+
});
88+
89+
test('Should create link with the absolute path from the included file', () => {
90+
const result = transformYfm(
91+
['', '{% include [create-folder](./mocks/absolute-link.md) %}', ''].join('\n'),
92+
);
93+
94+
expect(result).toEqual('<p><a href="/link/">Absolute link</a></p>\n');
95+
});
8096
});

test/mocks/absolute-link.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[Absolute link](/link/)

0 commit comments

Comments
 (0)