Skip to content

Commit e8074e6

Browse files
committed
fix(build): resolve nested md inclusions properly
closes #2584 closes #2586 Co-authored-by: Jeff Tian <[email protected]>
1 parent f60b32f commit e8074e6

File tree

6 files changed

+23
-5
lines changed

6 files changed

+23
-5
lines changed

Diff for: __tests__/e2e/markdown-extensions/markdown-extensions.test.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ describe('Table of Contents', () => {
6565
test('render toc', async () => {
6666
const items = page.locator('#table-of-contents + nav ul li')
6767
const count = await items.count()
68-
expect(count).toBe(33)
68+
expect(count).toBe(35)
6969
})
7070
})
7171

@@ -242,6 +242,15 @@ describe('Markdown File Inclusion', () => {
242242
expect(await h1.getAttribute('id')).toBe('foo-1')
243243
})
244244

245+
test('render markdown using nested inclusion inside sub folder', async () => {
246+
const h1 = page.locator('#after-foo + h1')
247+
expect(await h1.getAttribute('id')).toBe('inside-sub-folder')
248+
const h2 = page.locator('#after-foo + h1 + h2')
249+
expect(await h2.getAttribute('id')).toBe('sub-sub')
250+
const h3 = page.locator('#after-foo + h1 + h2 + h3')
251+
expect(await h3.getAttribute('id')).toBe('sub-sub-sub')
252+
})
253+
245254
test('support selecting range', async () => {
246255
const h2 = page.locator('#markdown-file-inclusion-with-range + h2')
247256
expect(trim(await h2.textContent())).toBe('Region')

Diff for: __tests__/e2e/markdown-extensions/nested-include.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
<!--@include: ./foo.md-->
22

33
### After Foo
4+
5+
<!--@include: ./subfolder/inside-subfolder.md-->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Inside sub folder
2+
3+
<!--@include: ./subsub/subsub.md-->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Sub sub
2+
3+
<!--@include: ./subsubsub/subsubsub.md-->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
### Sub sub sub

Diff for: src/node/markdownToVue.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export async function createMarkdownToVueRenderFn(
9090
// resolve includes
9191
let includes: string[] = []
9292

93-
function processIncludes(src: string): string {
93+
function processIncludes(src: string, file: string): string {
9494
return src.replace(includesRE, (m: string, m1: string) => {
9595
if (!m1.length) return m
9696

@@ -100,7 +100,7 @@ export async function createMarkdownToVueRenderFn(
100100
try {
101101
const includePath = atPresent
102102
? path.join(srcDir, m1.slice(m1[1] === '/' ? 2 : 1))
103-
: path.join(path.dirname(fileOrig), m1)
103+
: path.join(path.dirname(file), m1)
104104
let content = fs.readFileSync(includePath, 'utf-8')
105105
if (range) {
106106
const [, startLine, endLine] = range
@@ -114,14 +114,14 @@ export async function createMarkdownToVueRenderFn(
114114
}
115115
includes.push(slash(includePath))
116116
// recursively process includes in the content
117-
return processIncludes(content)
117+
return processIncludes(content, includePath)
118118
} catch (error) {
119119
return m // silently ignore error if file is not present
120120
}
121121
})
122122
}
123123

124-
src = processIncludes(src)
124+
src = processIncludes(src, fileOrig)
125125

126126
// reset env before render
127127
const env: MarkdownEnv = {

0 commit comments

Comments
 (0)