Skip to content

Commit 7096441

Browse files
fix: check real path for included file in case of symlink
1 parent ae8ad21 commit 7096441

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

src/transform/plugins/includes/index.ts

+16-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ import {bold} from 'chalk';
22
import Token from 'markdown-it/lib/token';
33

44
import {StateCore} from '../../typings';
5-
import {GetFileTokensOpts, getFileTokens, getFullIncludePath, isFileExists} from '../../utilsFS';
5+
import {
6+
GetFileTokensOpts,
7+
getFileTokens,
8+
getFullIncludePath,
9+
getRealPath,
10+
isFileExists,
11+
} from '../../utilsFS';
612
import {findBlockTokens} from '../../utils';
713
import {MarkdownItPluginCb, MarkdownItPluginOpts} from '../typings';
814

@@ -44,20 +50,22 @@ function unfoldIncludes(md: MarkdownItIncluded, state: StateCore, path: string,
4450

4551
const fullIncludePath = getFullIncludePath(includePath, root, path);
4652

47-
let pathname = fullIncludePath;
48-
let hash = '';
49-
const hashIndex = fullIncludePath.lastIndexOf('#');
50-
if (hashIndex > -1 && !isFileExists(pathname)) {
51-
pathname = fullIncludePath.slice(0, hashIndex);
52-
hash = fullIncludePath.slice(hashIndex + 1);
53-
}
53+
// Check the real path of the file in case of a symlink
54+
let pathname = getRealPath(fullIncludePath);
5455

5556
if (!pathname.startsWith(root)) {
5657
i++;
5758

5859
continue;
5960
}
6061

62+
let hash = '';
63+
const hashIndex = fullIncludePath.lastIndexOf('#');
64+
if (hashIndex > -1 && !isFileExists(pathname)) {
65+
pathname = fullIncludePath.slice(0, hashIndex);
66+
hash = fullIncludePath.slice(hashIndex + 1);
67+
}
68+
6169
// Check the existed included store and extract it
6270
const included = md.included?.[pathname];
6371

src/transform/utilsFS.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type {Dictionary} from 'lodash';
22

3-
import {readFileSync, statSync} from 'fs';
3+
import {readFileSync, realpathSync, statSync} from 'fs';
44
import escapeRegExp from 'lodash/escapeRegExp';
55
import {join, parse, relative, resolve, sep} from 'path';
66

@@ -168,3 +168,11 @@ export function getRelativePath(path: string, toPath: string) {
168168
const parentPath = pathDirs.join(sep);
169169
return relative(parentPath, toPath);
170170
}
171+
172+
export function getRealPath(symlinkPath: string): string {
173+
try {
174+
return realpathSync(symlinkPath);
175+
} catch (err) {
176+
return symlinkPath;
177+
}
178+
}

0 commit comments

Comments
 (0)