From 9fae64309068204d3976b877eaca3a8c48964d69 Mon Sep 17 00:00:00 2001 From: Fuxing Loh <4266087+fuxingloh@users.noreply.github.com> Date: Wed, 23 Aug 2023 10:21:26 +0800 Subject: [PATCH] chore(contented-pipeline-md): parse `%20` as space (#582) #### What this PR does / why we need it: Parse `%20` as space for `RemarkLink.ts`, also, add test for `truncate`. --- .../40-Section Title/200-DeepLink.md | 3 ++ .../fixtures/RemarkFrontmatter.md | 5 +++ .../fixtures/RemarkLink.md | 9 +++++ .../src/plugins/RemarkFrontmatter.unit.ts | 35 +++++++++++++++++++ .../src/plugins/RemarkLink.ts | 2 +- .../src/plugins/RemarkLink.unit.ts | 16 +++------ 6 files changed, 58 insertions(+), 12 deletions(-) create mode 100644 packages/contented-pipeline-md/fixtures/20-Heading/40-Section Title/200-DeepLink.md create mode 100644 packages/contented-pipeline-md/fixtures/RemarkFrontmatter.md create mode 100644 packages/contented-pipeline-md/src/plugins/RemarkFrontmatter.unit.ts diff --git a/packages/contented-pipeline-md/fixtures/20-Heading/40-Section Title/200-DeepLink.md b/packages/contented-pipeline-md/fixtures/20-Heading/40-Section Title/200-DeepLink.md new file mode 100644 index 00000000..7c729153 --- /dev/null +++ b/packages/contented-pipeline-md/fixtures/20-Heading/40-Section Title/200-DeepLink.md @@ -0,0 +1,3 @@ +# DeepLinke + +Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla ege. diff --git a/packages/contented-pipeline-md/fixtures/RemarkFrontmatter.md b/packages/contented-pipeline-md/fixtures/RemarkFrontmatter.md new file mode 100644 index 00000000..2d6f2b76 --- /dev/null +++ b/packages/contented-pipeline-md/fixtures/RemarkFrontmatter.md @@ -0,0 +1,5 @@ +# Remark Frontmatter + +This is a test file with frontmatter where `title` and `description` will automatically be extracted if they are +present and not explicitly set in the `---` configuration. This is useful for SEO purposes where descriptions and +titles are important but not always explicitly set in the markdown frontmatter. diff --git a/packages/contented-pipeline-md/fixtures/RemarkLink.md b/packages/contented-pipeline-md/fixtures/RemarkLink.md index 0c9e5c07..209914aa 100644 --- a/packages/contented-pipeline-md/fixtures/RemarkLink.md +++ b/packages/contented-pipeline-md/fixtures/RemarkLink.md @@ -1,4 +1,13 @@ +# `RemarkLink.ts` + +`RemarkLink.ts` will automatically link to other markdown files in your repository. + +## Test Links + - [20-Heading/100-RemarkLinked.md](20-Heading/100-RemarkLinked.md) - [01-RemarkLinked.md](01-RemarkLinked.md) - [RemarkLink.md](RemarkLink.md) - [Contented](https://contented.dev) +- [20-Heading/100-RemarkLinked.md](./20-Heading/100-RemarkLinked.md) +- [20-Heading/40-Section Title/200-DeepLink.md](./20-Heading/40-Section%20Title/200-DeepLink.md) +- [01-RemarkLinked.md](./01-RemarkLinked.md) diff --git a/packages/contented-pipeline-md/src/plugins/RemarkFrontmatter.unit.ts b/packages/contented-pipeline-md/src/plugins/RemarkFrontmatter.unit.ts new file mode 100644 index 00000000..d01210be --- /dev/null +++ b/packages/contented-pipeline-md/src/plugins/RemarkFrontmatter.unit.ts @@ -0,0 +1,35 @@ +import { join } from 'node:path'; + +import { MarkdownPipeline } from '../MarkdownPipeline'; + +const rootPath = join(__dirname, '../../fixtures'); + +const pipeline = new MarkdownPipeline(__dirname, { + type: 'Markdown', + pattern: ['/20-Heading/*.md', '/01-RemarkLinked.md', '/RemarkLink.md'], + processor: 'md', +}); + +beforeAll(async () => { + await pipeline.init(); +}); + +it('should process RemarkFrontmatter.md', async () => { + const content = await pipeline.process(rootPath, 'RemarkFrontmatter.md'); + expect(content).toStrictEqual([ + { + type: 'Markdown', + fields: { + title: 'Remark Frontmatter', + description: + 'This is a test file with frontmatter where title and description will automatically be extracted if they are present and not explicitly set in the --- configuration. This is useful for SEO purposes where descriptions and titles are important but n...', + }, + headings: expect.any(Array), + sections: [], + path: '/remark-frontmatter', + fileId: expect.stringMatching(/[0-f]{64}/), + modifiedDate: expect.any(Number), + html: expect.any(String), + }, + ]); +}); diff --git a/packages/contented-pipeline-md/src/plugins/RemarkLink.ts b/packages/contented-pipeline-md/src/plugins/RemarkLink.ts index 164e9ea8..735f29e9 100644 --- a/packages/contented-pipeline-md/src/plugins/RemarkLink.ts +++ b/packages/contented-pipeline-md/src/plugins/RemarkLink.ts @@ -31,7 +31,7 @@ function visitLink(file: VFile): (node: Link) => void { ); const parsedPath = parsePath(join(pipelineDir, contented.filePath)); const linkedFilePath = join(pipelineDir, path); - const relativePath = relative(parsedPath.dir, linkedFilePath); + const relativePath = relative(parsedPath.dir, linkedFilePath).replaceAll('%20', ' '); node.url = contented.contentedPipeline.getSanitizedPath(relativePath); } else { node.url = contented.contentedPipeline.getSanitizedPath(path); diff --git a/packages/contented-pipeline-md/src/plugins/RemarkLink.unit.ts b/packages/contented-pipeline-md/src/plugins/RemarkLink.unit.ts index c06b0beb..e964165a 100644 --- a/packages/contented-pipeline-md/src/plugins/RemarkLink.unit.ts +++ b/packages/contented-pipeline-md/src/plugins/RemarkLink.unit.ts @@ -20,21 +20,15 @@ it('should process RemarkLink.md', async () => { { type: 'Markdown', fields: { - description: '20-Heading/100-RemarkLinked.md', - title: undefined, + title: 'RemarkLink.ts', + description: 'RemarkLink.ts will automatically link to other markdown files in your repository.', }, - headings: [], - path: '/remark-link', + headings: expect.any(Array), sections: [], + path: '/remark-link', fileId: expect.stringMatching(/[0-f]{64}/), modifiedDate: expect.any(Number), - html: - '', + html: '

RemarkLink.ts

\n

RemarkLink.ts will automatically link to other markdown files in your repository.

\n\n', }, ]); });