Skip to content

Commit 1720a1e

Browse files
feat: support rendering raw html from special code blocks
1 parent a04facd commit 1720a1e

File tree

3 files changed

+48
-37
lines changed

3 files changed

+48
-37
lines changed

README.md

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,29 @@ Success! 🚀
2323

2424
The following [Notion API block object types](https://developers.notion.com/reference/block) are supported:
2525

26-
| Block Type | Supported | Notes |
27-
| ----------------- | ------------- | -------------------------------------------------------------------- |
28-
| Paragraph | ✅ Yes | |
29-
| Heading1-3 | ✅ Yes | |
30-
| Callout | ✅ Yes | |
31-
| Quote | ✅ Yes | |
32-
| Bulleted List | ✅ Yes | |
33-
| Numbered List | ✅ Yes | |
34-
| To do | ✅ Yes | |
35-
| Toggle | ✅ (Yes) | Toggle content is included, however the toggle header is not |
36-
| Code | ✅ Yes | |
37-
| Child Pages | ❌ not planned | avoid, they don't mix well with clear site navigation |
38-
| Child Databases | ✅ Yes | renders as table + including child pages, inline-only tables planned |
39-
| Embed | ❌ Missing | unclear, might be undesireable for static sites |
40-
| Image | ✅ (Yes) | captions not supported yet |
41-
| Video | ❌ Missing | |
42-
| File | ❌ Missing | |
43-
| PDF | ❌ Missing | |
44-
| Bookmark | ❌ Missing | |
45-
| Equation | ❌ Missing | |
46-
| Divider | ✅ Yes | |
47-
| Table Of Contents | ❌ not planned | static site generators have their own ToC implementations |
48-
| Breadcrumb | ❌ not planned | static site generators have their own nav implementations |
26+
| Block Type | Supported | Notes |
27+
| ----------------- | ------------- | -------------------------------------------------------------------------------------------------------------- |
28+
| Paragraph | ✅ Yes | |
29+
| Heading1-3 | ✅ Yes | |
30+
| Callout | ✅ Yes | |
31+
| Quote | ✅ Yes | |
32+
| Bulleted List | ✅ Yes | |
33+
| Numbered List | ✅ Yes | |
34+
| To do | ✅ Yes | |
35+
| Toggle | ✅ (Yes) | Toggle content is included, however the toggle header is not |
36+
| Code | ✅ Yes | An html block starting with `<!--notion-markdown-cms:raw-->` is rendered as raw HTML and not as a fenced block |
37+
| Child Pages | ❌ not planned | avoid, they don't mix well with clear site navigation |
38+
| Child Databases | ✅ Yes | renders as table + including child pages, inline-only tables planned |
39+
| Embed | ❌ Missing | unclear, might be undesireable for static sites |
40+
| Image | ✅ (Yes) | captions not supported yet |
41+
| Video | ❌ Missing | |
42+
| File | ❌ Missing | |
43+
| PDF | ❌ Missing | |
44+
| Bookmark | ❌ Missing | |
45+
| Equation | ❌ Missing | |
46+
| Divider | ✅ Yes | |
47+
| Table Of Contents | ❌ not planned | static site generators have their own ToC implementations |
48+
| Breadcrumb | ❌ not planned | static site generators have their own nav implementations |
4949

5050
Support for other block types can be considered once they are available on the official Notion API.
5151

src/BlockRenderer.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ export class BlockRenderer {
6868
switch (block.type) {
6969
case "paragraph":
7070
return {
71-
lines: await this.richText.renderMarkdown(block.paragraph.text)
72-
}
71+
lines: await this.richText.renderMarkdown(block.paragraph.text),
72+
};
7373
// note: render headings +1 level, because h1 is reserved for page titles
7474
case "heading_1":
7575
return {
@@ -107,12 +107,16 @@ export class BlockRenderer {
107107
lines: "> " + (await this.richText.renderMarkdown((block as any).quote.text))
108108
};
109109
case "code":
110+
const code = await this.richText.renderPlainText(block.code.text);
111+
if (code.startsWith("<!--notion-markdown-cms:raw-->")) {
112+
return { lines: code };
113+
}
114+
110115
return {
111-
lines:
116+
lines:
112117
"```" +
113118
block.code.language +
114-
"\n" +
115-
(await this.richText.renderMarkdown(block.code.text)) +
119+
"\n" + code +
116120
"\n```"
117121
};
118122
case "callout":
@@ -121,7 +125,7 @@ export class BlockRenderer {
121125
"> " +
122126
this.renderIcon(block.callout.icon) +
123127
" " +
124-
(await this.richText.renderMarkdown(block.callout.text))
128+
(await this.richText.renderMarkdown(block.callout.text)),
125129
};
126130
case "divider":
127131
return { lines: "---" };

src/RichTextRenderer.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1-
import { RichText } from "@notionhq/client/build/src/api-types";
2-
import { RenderDatabasePageTask } from "./RenderDatabasePageTask";
3-
import { logger } from "./logger";
4-
import { MentionedPageRenderer } from "./MentionedPageRenderer";
5-
import { LinkRenderer } from "./LinkRenderer";
1+
import { RichText } from '@notionhq/client/build/src/api-types';
2+
3+
import { LinkRenderer } from './LinkRenderer';
4+
import { logger } from './logger';
5+
import { MentionedPageRenderer } from './MentionedPageRenderer';
6+
import { RenderDatabasePageTask } from './RenderDatabasePageTask';
67

78
const debug = require("debug")("richtext");
89

910
export class RichTextRenderer {
1011
constructor(
1112
private readonly mentionedPageRenderer: MentionedPageRenderer,
1213
private readonly linkRenderer: LinkRenderer
13-
) {}
14+
) {}
1415

16+
public async renderPlainText(text: RichText[]): Promise<string> {
17+
return text.map((rt) => rt.plain_text).join(" ");
18+
}
19+
1520
public async renderMarkdown(text: RichText[]): Promise<string> {
1621
const result: string[] = [];
1722

@@ -99,7 +104,9 @@ export class RichTextRenderer {
99104
return mod;
100105
}
101106

102-
private async resolveMentionedPage(id: string): Promise<RenderDatabasePageTask | null> {
107+
private async resolveMentionedPage(
108+
id: string
109+
): Promise<RenderDatabasePageTask | null> {
103110
return await this.mentionedPageRenderer.renderPage(id);
104111
}
105112

@@ -108,7 +115,7 @@ export class RichTextRenderer {
108115

109116
return `${modifier}${content.trim()}${reversedMod}`;
110117
}
111-
118+
112119
private renderUnsupported(msg: string, obj: any): string {
113120
logger.warn(msg);
114121
debug(msg + "\n%O", obj);

0 commit comments

Comments
 (0)