Core package for converting Notion pages to Markdown.
# if JavaScript
npm install @notion-md-converter/core
# if TypeScript
npm install @notion-md-converter/core @notion-md-converter/types
Follow Notion's Getting Started Guide to obtain an API key.
import {
$getPageFullContent,
NotionMarkdownConverter,
} from "@notion-md-converter/core";
import { Client } from "@notionhq/client";
const client = new Client({
auth: API_KEY,
});
const pageId = "some-page-id";
// Notion API helpers in this library.
// Recursively retrieve the Notion Block's child elements
const content = await $getPageFullContent(client, pageId);
// convert to markdwon
const executor = new NotionMarkdownConverter();
const result = executor.execute(content);
[!WARNING] The APIs
$getPageFullContent
and$getDatabasePages
may undergo specification changes in the future as we plan to remove the dependency on@notionhq/client
.
If you want to change the conversion of a Heading Block.
For example, define a custom transformer that increases the number of #
in a Markdown heading by one.
import { createHeadingTransformerFactory, MarkdownUtils } from "@notion-md-converter/core";
export const createMarkdownCustomHeadingTransformer = () => {
// Use a function to create a transformer
return createHeadingTransformerFactory(({ level, richText }) => {
const text = MarkdownUtils.convertRichTextsToMarkdown(richText);
return MarkdownUtils.wrapWithNewLines(MarkdownUtils.heading(text, level + 1)); // add 1 level
});
};
To simplify writing tests for transformers, we provide the @notion-md-converter/testing
library.
This library allows you to easily create Notion block objects and test their conversion results.
$ npm install @notion-md-converter/testing
import {
createTransformerContext,
createHeading1Block,
createTextRichText,
dedent,
} from "@notion-md-converter/testing";
import { createMarkdownCustomHeadingTransformer } from "./createMarkdownCustomHeadingTransformer";
describe("createMarkdownCustomHeadingTransformer", () => {
const transformer = createMarkdownCustomHeadingTransformer();
it("Can convert heading_1 block", () => {
const block = createHeading1Block({
richText: [
createTextRichText({
content: "Hello",
}),
],
});
const context = createTransformerContext({
blocks: [block],
});
const result = transformer(context);
expect(result).toBe(dedent({ wrap: true })`
## Hello
`);
});
});
Define the created transformer in the options of the converter.
const executor = new NotionMarkdownConverter({
heading: createMarkdownCustomHeadingTransformer(),
});
const result = executor.execute(content);
You can set metadata for captions in blocks such as images, code blocks, and embeds. Metadata is specified in key=value
format, and the portion from the beginning of the caption to the first :
is treated as metadata.
width=500:This is an image description
In this case:
-
width=500
is the metadata -
This is an image description
is the actual caption
Multiple metadata can be specified by separating them with &
:
width=500&height=300:This is an image description
In this case:
-
width=500
andheight=300
are metadata -
This is an image description
is the actual caption
- Specifying image width:
width=500:Image description
- Setting diff for code blocks:
diff=true:filename.js
Distributed under the MIT License. See LICENSE for more information.
malvageee (https://github.com/salvage0707)