From bbdfb4de13568be057b446bb6dbed71267b80d96 Mon Sep 17 00:00:00 2001 From: Fuxing Loh <4266087+fuxingloh@users.noreply.github.com> Date: Fri, 21 Jul 2023 16:39:34 +0800 Subject: [PATCH] feat(contented): add filePath and Edit on GitHub (#525) #### What this PR does / why we need it: As per title. --- .../contented-example/contented.config.js | 6 +++++ .../src/MarkdownPipeline.ts | 5 +++-- .../src/plugins/Plugin.ts | 1 + .../src/pages/[[...slug]].page.jsx | 2 +- .../src/pages/_components/ContentHeadings.jsx | 22 +++++++++++++++++-- 5 files changed, 31 insertions(+), 5 deletions(-) diff --git a/packages/contented-example/contented.config.js b/packages/contented-example/contented.config.js index bf0825fc..277209f3 100644 --- a/packages/contented-example/contented.config.js +++ b/packages/contented-example/contented.config.js @@ -25,6 +25,12 @@ const config = { tags: { type: 'string[]', }, + editOnGitHubLink: { + type: 'string', + resolve: (_, { file }) => { + return `https://github.com/levaintech/contented/edit/main/packages/contented-example/${file.data.contented.filePath}`; + }, + }, }, transform: (file) => { file.path = file.path.replaceAll(/^\/docs\/?/g, '/'); diff --git a/packages/contented-pipeline-md/src/MarkdownPipeline.ts b/packages/contented-pipeline-md/src/MarkdownPipeline.ts index 1224db14..1a9b79a5 100644 --- a/packages/contented-pipeline-md/src/MarkdownPipeline.ts +++ b/packages/contented-pipeline-md/src/MarkdownPipeline.ts @@ -26,7 +26,7 @@ export class MarkdownPipeline extends ContentedPipeline { return []; } - vFile.data = { contented: this.newUnifiedContented() }; + vFile.data = { contented: this.newUnifiedContented(file) }; const output = await this.processor.process(vFile); const contented = output.data.contented as UnifiedContented; @@ -44,13 +44,14 @@ export class MarkdownPipeline extends ContentedPipeline { return read(join(rootPath, file)); } - protected newUnifiedContented(): UnifiedContented { + protected newUnifiedContented(filePath: string): UnifiedContented { return { contentedPipeline: this, pipeline: this.pipeline, headings: [], fields: {}, errors: [], + filePath, }; } diff --git a/packages/contented-pipeline-md/src/plugins/Plugin.ts b/packages/contented-pipeline-md/src/plugins/Plugin.ts index b0f700d7..ccb1b994 100644 --- a/packages/contented-pipeline-md/src/plugins/Plugin.ts +++ b/packages/contented-pipeline-md/src/plugins/Plugin.ts @@ -12,4 +12,5 @@ export interface UnifiedContented { type: string; reason: string; }[]; + filePath: string; } diff --git a/packages/contented-preview/src/pages/[[...slug]].page.jsx b/packages/contented-preview/src/pages/[[...slug]].page.jsx index b580b9ea..a3fdd008 100644 --- a/packages/contented-preview/src/pages/[[...slug]].page.jsx +++ b/packages/contented-preview/src/pages/[[...slug]].page.jsx @@ -92,7 +92,7 @@ export default function SlugPage({ content, sections }) {
diff --git a/packages/contented-preview/src/pages/_components/ContentHeadings.jsx b/packages/contented-preview/src/pages/_components/ContentHeadings.jsx index e4eb1e84..22e9fd39 100644 --- a/packages/contented-preview/src/pages/_components/ContentHeadings.jsx +++ b/packages/contented-preview/src/pages/_components/ContentHeadings.jsx @@ -4,14 +4,32 @@ import Link from 'next/link'; export default function ContentHeadings(props) { return ( <> - {props.tags?.length > 0 && ( + {props.fields.editOnGitHubLink && ( +
+ + Edit on GitHub + + + + +
+ )} + {props.fields.tags?.length > 0 && ( <>

Tags

- {props.tags.map((tag) => ( + {props.fields.tags.map((tag) => (