Skip to content

Commit

Permalink
fix: absolute links in included files
Browse files Browse the repository at this point in the history
  • Loading branch information
vsesh authored and 3y3 committed Sep 4, 2023
1 parent 86e833a commit cefe20a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/transform/plugins/links/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {PAGE_LINK_REGEXP} from './constants';
import Token from 'markdown-it/lib/token';
import {Logger} from 'src/transform/log';
import {MarkdownItPluginCb, MarkdownItPluginOpts} from '../typings';
import path, {parse, relative, resolve} from 'path';
import path, {isAbsolute, parse, relative, resolve} from 'path';
import {StateCore} from 'src/transform/typings';

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

if (isAbsolute(href)) {
return;
}

if (pathname) {
file = resolve(path.parse(currentPath).dir, pathname);
fileExists = isFileExists(file);
Expand Down
16 changes: 16 additions & 0 deletions test/links.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,20 @@ describe('Links', () => {

expect(result).toEqual('<h1>First</h1>\n<p><a href="./second.html">Second</a></p>\n');
});

test('Should create link with the absolute path', () => {
const inputPath = resolve(__dirname, './mocks/absolute-link.md');
const input = readFileSync(inputPath, 'utf8');
const result = transformYfm(input, inputPath);

expect(result).toEqual('<p><a href="/link/">Absolute link</a></p>\n');
});

test('Should create link with the absolute path from the included file', () => {
const result = transformYfm(
['', '{% include [create-folder](./mocks/absolute-link.md) %}', ''].join('\n'),
);

expect(result).toEqual('<p><a href="/link/">Absolute link</a></p>\n');
});
});
1 change: 1 addition & 0 deletions test/mocks/absolute-link.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[Absolute link](/link/)

0 comments on commit cefe20a

Please sign in to comment.