Skip to content

Commit

Permalink
fix(include): support paths with sharp symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
oynikishin committed Nov 29, 2022
1 parent d0a741f commit a19bd3c
Show file tree
Hide file tree
Showing 6 changed files with 431 additions and 6 deletions.
10 changes: 7 additions & 3 deletions src/transform/plugins/includes/collect.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {relative} from 'path';
import {bold} from 'chalk';

import {resolveRelativePath} from '../../utilsFS';
import {isFileExists, resolveRelativePath} from '../../utilsFS';
import {MarkdownItPluginOpts} from '../typings';

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

relativePath = relativePath.split('#')[0];
let includePath = resolveRelativePath(path, relativePath);
const hashIndex = relativePath.lastIndexOf('#');
if (hashIndex > -1 && !isFileExists(includePath)) {
includePath = includePath.slice(0, includePath.lastIndexOf('#'));
relativePath = relativePath.slice(0, hashIndex);
}

const includePath = resolveRelativePath(path, relativePath);
const targetDestPath = resolveRelativePath(destPath, relativePath);

if (includesPaths.includes(includePath)) {
Expand Down
11 changes: 9 additions & 2 deletions src/transform/plugins/includes/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {bold} from 'chalk';

import {getFileTokens, GetFileTokensOpts, getFullIncludePath} from '../../utilsFS';
import {getFileTokens, GetFileTokensOpts, getFullIncludePath, isFileExists} from '../../utilsFS';
import {findBlockTokens} from '../../utils';
import Token from 'markdown-it/lib/token';
import {MarkdownItPluginCb, MarkdownItPluginOpts} from '../typings';
Expand Down Expand Up @@ -39,8 +39,15 @@ function unfoldIncludes(state: StateCore, path: string, options: Options) {
) {
try {
const [, keyword /* description */, , includePath] = match;

const fullIncludePath = getFullIncludePath(includePath, root, path);
const [pathname, hash] = fullIncludePath.split('#');
let pathname = fullIncludePath;
let hash = '';
const hashIndex = fullIncludePath.lastIndexOf('#');
if (hashIndex > -1 && !isFileExists(pathname)) {
pathname = fullIncludePath.slice(0, hashIndex);
hash = fullIncludePath.slice(hashIndex + 1);
}

if (!pathname.startsWith(root)) {
i++;
Expand Down
17 changes: 17 additions & 0 deletions test/anchors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,21 @@ describe('Anchors', () => {
'<p>Content</p>\n',
);
});

it('should include content by anchor in sharped path file', () => {
expect(
transformYfm(
'Content before include\n' +
'\n' +
'{% include [file](./mocks/folder-with-#-sharp/file-with-#-sharp.md#anchor) %}\n' +
'\n' +
'After include',
),
).toBe(
'<p>Content before include</p>\n' +
'<h2 id="anchor"><a href="#anchor" class="yfm-anchor" aria-hidden="true"></a>Subtitle</h2>\n' +
'<p>Subcontent</p>\n' +
'<p>After include</p>\n',
);
});
});
Loading

0 comments on commit a19bd3c

Please sign in to comment.