Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check real path for included file in case of symlink #566

Merged
merged 2 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions src/transform/plugins/includes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import {bold} from 'chalk';
import Token from 'markdown-it/lib/token';

import {StateCore} from '../../typings';
import {GetFileTokensOpts, getFileTokens, getFullIncludePath, isFileExists} from '../../utilsFS';
import {
GetFileTokensOpts,
getFileTokens,
getFullIncludePath,
getRealPath,
isFileExists,
} from '../../utilsFS';
import {findBlockTokens} from '../../utils';
import {MarkdownItPluginCb, MarkdownItPluginOpts} from '../typings';

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

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

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

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

continue;
}

let hash = '';
const hashIndex = fullIncludePath.lastIndexOf('#');
if (hashIndex > -1 && !isFileExists(pathname)) {
pathname = fullIncludePath.slice(0, hashIndex);
hash = fullIncludePath.slice(hashIndex + 1);
}

// Check the existed included store and extract it
const included = md.included?.[pathname];

Expand Down
10 changes: 9 additions & 1 deletion src/transform/utilsFS.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {Dictionary} from 'lodash';

import {readFileSync, statSync} from 'fs';
import {readFileSync, realpathSync, statSync} from 'fs';
import escapeRegExp from 'lodash/escapeRegExp';
import {join, parse, relative, resolve, sep} from 'path';

Expand Down Expand Up @@ -168,3 +168,11 @@ export function getRelativePath(path: string, toPath: string) {
const parentPath = pathDirs.join(sep);
return relative(parentPath, toPath);
}

export function getRealPath(symlinkPath: string): string {
try {
return realpathSync(symlinkPath);
} catch (err) {
return symlinkPath;
}
}
71 changes: 70 additions & 1 deletion test/includes.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,86 @@
import {dirname} from 'path';
import {readFile, symlink, unlink} from 'node:fs/promises';
import {dirname, resolve} from 'path';
import dedent from 'ts-dedent';

import transform from '../src/transform';
import includes from '../src/transform/plugins/includes';
import yfmlint from '../src/transform/yfmlint';
import {log} from '../src/transform/log';

import {callPlugin, tokenize} from './utils';
import {codeInBackQuote, notitle, sharpedFile, title} from './data/includes';

const mocksPath = require.resolve('./mocks/link.md');
const symLinkPath = resolve(__dirname, './mocks/symlink.md');
const transformYfm = (text: string) => {
const {
result: {html},
} = transform(text, {
plugins: [includes],
path: mocksPath,
root: dirname(mocksPath),
});
return html;
};

describe('Includes', () => {
beforeEach(() => {
log.clear();
});

test('Simple include', async () => {
const expectPath = resolve(__dirname, './mocks/include.expect.html');
const expectContent = await readFile(expectPath, 'utf8');

const html = transformYfm(dedent`
start main

{% include [test](./include.md) %}

end main
`);

expect(html).toBe(expectContent);
});

test('Symlink include', async () => {
const expectPath = resolve(__dirname, './mocks/include.expect.html');
const expectContent = await readFile(expectPath, 'utf8');

await symlink(resolve(__dirname, './mocks/include.md'), symLinkPath);

const html = transformYfm(dedent`
start main

{% include [test](./symlink.md) %}

end main
`);

expect(html).toBe(expectContent);

await unlink(symLinkPath);
});

test('Include symlink outside root', async () => {
const expectPath = resolve(__dirname, './mocks/include-outside-symlink.expect.html');
const expectContent = await readFile(expectPath, 'utf8');

await symlink(resolve(__dirname, 'includes.test.ts'), symLinkPath);

const html = transformYfm(dedent`
start main

{% include [test](./symlink.md) %}

end main
`);

expect(html).toBe(expectContent);

await unlink(symLinkPath);
});

test('Should include with title', () => {
const mocksPath = require.resolve('./utils.ts');

Expand Down
3 changes: 3 additions & 0 deletions test/mocks/include-outside-symlink.expect.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<p>start main</p>
<p>{% include <a href="./symlink.md">test</a> %}</p>
<p>end main</p>
4 changes: 4 additions & 0 deletions test/mocks/include.expect.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<p>start main</p>
<h1>Title</h1>
<p>Content</p>
<p>end main</p>
Loading