Skip to content

Commit a19bd3c

Browse files
committed
fix(include): support paths with sharp symbol
1 parent d0a741f commit a19bd3c

File tree

6 files changed

+431
-6
lines changed

6 files changed

+431
-6
lines changed

src/transform/plugins/includes/collect.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {relative} from 'path';
22
import {bold} from 'chalk';
33

4-
import {resolveRelativePath} from '../../utilsFS';
4+
import {isFileExists, resolveRelativePath} from '../../utilsFS';
55
import {MarkdownItPluginOpts} from '../typings';
66

77
const includesPaths: string[] = [];
@@ -23,9 +23,13 @@ const collect = (input: string, options: Opts) => {
2323
let [, , , relativePath] = match;
2424
const [matchedInclude] = match;
2525

26-
relativePath = relativePath.split('#')[0];
26+
let includePath = resolveRelativePath(path, relativePath);
27+
const hashIndex = relativePath.lastIndexOf('#');
28+
if (hashIndex > -1 && !isFileExists(includePath)) {
29+
includePath = includePath.slice(0, includePath.lastIndexOf('#'));
30+
relativePath = relativePath.slice(0, hashIndex);
31+
}
2732

28-
const includePath = resolveRelativePath(path, relativePath);
2933
const targetDestPath = resolveRelativePath(destPath, relativePath);
3034

3135
if (includesPaths.includes(includePath)) {

src/transform/plugins/includes/index.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {bold} from 'chalk';
22

3-
import {getFileTokens, GetFileTokensOpts, getFullIncludePath} from '../../utilsFS';
3+
import {getFileTokens, GetFileTokensOpts, getFullIncludePath, isFileExists} from '../../utilsFS';
44
import {findBlockTokens} from '../../utils';
55
import Token from 'markdown-it/lib/token';
66
import {MarkdownItPluginCb, MarkdownItPluginOpts} from '../typings';
@@ -39,8 +39,15 @@ function unfoldIncludes(state: StateCore, path: string, options: Options) {
3939
) {
4040
try {
4141
const [, keyword /* description */, , includePath] = match;
42+
4243
const fullIncludePath = getFullIncludePath(includePath, root, path);
43-
const [pathname, hash] = fullIncludePath.split('#');
44+
let pathname = fullIncludePath;
45+
let hash = '';
46+
const hashIndex = fullIncludePath.lastIndexOf('#');
47+
if (hashIndex > -1 && !isFileExists(pathname)) {
48+
pathname = fullIncludePath.slice(0, hashIndex);
49+
hash = fullIncludePath.slice(hashIndex + 1);
50+
}
4451

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

test/anchors.test.ts

+17
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,21 @@ describe('Anchors', () => {
9494
'<p>Content</p>\n',
9595
);
9696
});
97+
98+
it('should include content by anchor in sharped path file', () => {
99+
expect(
100+
transformYfm(
101+
'Content before include\n' +
102+
'\n' +
103+
'{% include [file](./mocks/folder-with-#-sharp/file-with-#-sharp.md#anchor) %}\n' +
104+
'\n' +
105+
'After include',
106+
),
107+
).toBe(
108+
'<p>Content before include</p>\n' +
109+
'<h2 id="anchor"><a href="#anchor" class="yfm-anchor" aria-hidden="true"></a>Subtitle</h2>\n' +
110+
'<p>Subcontent</p>\n' +
111+
'<p>After include</p>\n',
112+
);
113+
});
97114
});

0 commit comments

Comments
 (0)