-
Notifications
You must be signed in to change notification settings - Fork 35
Created a plugin for frontmatter #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
47ba279
c643f23
e37e09b
483f531
ced39ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
import { GetPageResponse } from "@notionhq/client/build/src/api-endpoints"; | ||
import { NotionPage } from "../NotionPage"; | ||
import { standardFrontmatterTransformer } from "./FronmatterTransformer"; | ||
|
||
let getFrontMatter = standardFrontmatterTransformer.frontmatterTransform | ||
?.build as (page: NotionPage) => string; | ||
|
||
const sampleMetadata: GetPageResponse = { | ||
object: "page", | ||
id: "6e6921b9-b1f5-4614-ab3c-bf1a73358a1f", | ||
created_time: "2023-04-11T10:17:00.000Z", | ||
last_edited_time: "2023-04-13T20:24:00.000Z", | ||
created_by: { | ||
object: "user", | ||
id: "USERID", | ||
}, | ||
last_edited_by: { | ||
object: "user", | ||
id: "USERID", | ||
}, | ||
cover: null, | ||
icon: { | ||
type: "file", | ||
file: { | ||
url: "https:/dummy_URL", | ||
expiry_time: "2023-04-15T11:50:20.461Z", | ||
}, | ||
}, | ||
parent: { | ||
type: "workspace", | ||
workspace: true, | ||
}, | ||
archived: false, | ||
properties: { | ||
title: { | ||
id: "title", | ||
type: "title", | ||
title: [ | ||
{ | ||
type: "text", | ||
text: { | ||
content: "Foo", | ||
link: null, | ||
}, | ||
annotations: { | ||
bold: false, | ||
italic: false, | ||
strikethrough: false, | ||
underline: false, | ||
code: false, | ||
color: "default", | ||
}, | ||
plain_text: "Foo", | ||
href: null, | ||
}, | ||
{ | ||
type: "text", | ||
text: { | ||
content: "Bar", | ||
link: null, | ||
}, | ||
annotations: { | ||
bold: false, | ||
italic: false, | ||
strikethrough: false, | ||
underline: false, | ||
code: false, | ||
color: "default", | ||
}, | ||
plain_text: "Bar", | ||
href: null, | ||
}, | ||
], | ||
}, | ||
Keywords: { | ||
id: "keywords", | ||
type: "rich_text", | ||
rich_text: [ | ||
{ | ||
type: "text", | ||
text: { | ||
content: "Foo, Bar", | ||
link: null, | ||
}, | ||
annotations: { | ||
bold: false, | ||
italic: false, | ||
strikethrough: false, | ||
underline: false, | ||
code: false, | ||
color: "default", | ||
}, | ||
plain_text: "Foo, Bar", | ||
href: null, | ||
}, | ||
], | ||
}, | ||
date_property: { | ||
id: "a%3Cql", | ||
type: "date", | ||
date: { | ||
start: "2021-10-24", | ||
end: "2021-10-28", | ||
time_zone: null, | ||
}, | ||
}, | ||
}, | ||
url: "https://www.notion.so/Site-docu-notion-PAGEID", | ||
}; | ||
|
||
describe("getFrontMatter", () => { | ||
let page: NotionPage; | ||
|
||
beforeEach(() => { | ||
page = new NotionPage({ | ||
layoutContext: "Test Context", | ||
pageId: "123", | ||
order: 1, | ||
metadata: JSON.parse(JSON.stringify(sampleMetadata)), | ||
foundDirectlyInOutline: true, | ||
}); | ||
}); | ||
|
||
it("should generate frontmatter with all available properties", () => { | ||
const expectedFrontmatter = `title: FooBar\nsidebar_position: 1\nslug: /123\nkeywords: [Foo, Bar]\n`; | ||
(page.metadata as any).properties.Keywords.rich_text[0].plain_text = | ||
"Foo, Bar"; | ||
|
||
const result = getFrontMatter(page); | ||
|
||
expect(result).toEqual(expectedFrontmatter); | ||
}); | ||
|
||
// "title: Foo-Barsidebar_position: 1slug: keywords: [Foo, Bar]" | ||
// "title: FooBar\nsidebar_position: 1\nslug: /123\n" | ||
it("should generate frontmatter with no keywords", () => { | ||
const expectedFrontmatter = `title: FooBar\nsidebar_position: 1\nslug: /123\n`; | ||
(page.metadata as any).properties.Keywords = undefined; | ||
|
||
const result = getFrontMatter(page); | ||
|
||
expect(result).toEqual(expectedFrontmatter); | ||
}); | ||
|
||
it("should replace colons with dashes in the title", () => { | ||
const expectedFrontmatter = `title: FooBaz-\nsidebar_position: 1\nslug: /123\nkeywords: [Foo, Bar]\n`; | ||
(page.metadata as any).properties.title.title[1].plain_text = "Baz:"; | ||
|
||
const result = getFrontMatter(page); | ||
|
||
expect(result).toEqual(expectedFrontmatter); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { IPlugin } from "./pluginTypes"; | ||
import { NotionPage } from "../NotionPage"; | ||
|
||
function getFrontmatter(page: NotionPage): string { | ||
let frontmatter = ""; | ||
frontmatter += `title: ${page.nameOrTitle.replaceAll(":", "-")}\n`; // I have not found a way to escape colons | ||
frontmatter += `sidebar_position: ${page.order}\n`; | ||
frontmatter += `slug: ${page.slug ?? ""}\n`; | ||
if (page.keywords) frontmatter += `keywords: [${page.keywords}]\n`; | ||
|
||
return frontmatter; | ||
} | ||
|
||
export const standardFrontmatterTransformer: IPlugin = { | ||
name: "standardFrontmatterTransformer", | ||
|
||
frontmatterTransform: { | ||
build: getFrontmatter, | ||
}, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,11 @@ export type IPlugin = { | |
// simple regex replacements on the markdown output | ||
regexMarkdownModifications?: IRegexMarkdownModification[]; | ||
|
||
// operations on pages to define the markdown's frontmatter | ||
frontmatterTransform?: { | ||
build: (page: NotionPage) => string; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Recently I broke the plugin API when someone needed the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I definitely need the context in a lot of places. Due to the complexity of interconnected documents, providing the full context makes it easier to write custom plugins without the complexity of figuring out where to get the context you need. |
||
}; | ||
|
||
// Allow a plugin to perform an async operation at the start of docu-notion. | ||
// Notice that the plugin itself is given, so you can add things to it. | ||
init?(plugin: IPlugin): Promise<void>; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not completely sold on the
frontmatterTransform
name. Can you think of a better one?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about
frontMatterGenerator
? I'm camel casing "Matter" becausefront matter
is normally written as two words.