Skip to content

Commit

Permalink
feat: Add --require-slugs option
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-polk committed Apr 3, 2024
1 parent 5d00de3 commit bff12fd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ One of the big attractions of Notion for large documentation projects is that yo
## Slugs

By default, pages will be given a slug based on the Notion id. For a human-readable URL, add a notion property named `Slug` to your database pages and enter a value in there that will work well in a URL. That is, no spaces, ?, #, /, etc.
By default, pages will be given a slug based on the Notion ID. For a human-readable URL, add a notion property named `Slug` to your database pages and enter a value in there that will work well in a URL. That is, no spaces, ?, #, /, etc.

See `Options` to require slugs in Notion.

## Known Limitations

Expand Down Expand Up @@ -130,7 +132,8 @@ Options:
| -l, --log-level <level> | | Log level (choices: `info`, `verbose`, `debug`) |
| -i, --img-output-path <string> | | Path to directory where images will be stored. If this is not included, images will be placed in the same directory as the document that uses them, which then allows for localization of screenshots. |
| -p, --img-prefix-in-markdown <string> | | When referencing an image from markdown, prefix with this path instead of the full img-output-path. Should be used only in conjunction with --img-output-path. |
| -h, --help | | display help for command |
| --require-slugs | | If set, docu-notion will fail if any pages it would otherwise publish are missing a slug in Notion. |
| -h, --help | | display help for command |

# Plugins

Expand Down
11 changes: 11 additions & 0 deletions src/pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export type DocuNotionOptions = {
imgOutputPath: string;
imgPrefixInMarkdown: string;
statusTag: string;
requireSlugs?: boolean;
};

let layoutStrategy: LayoutStrategy;
Expand All @@ -49,6 +50,7 @@ const counts = {
skipped_because_empty: 0,
skipped_because_status: 0,
skipped_because_level_cannot_have_content: 0,
error_because_no_slug: 0,
};

export async function notionPull(options: DocuNotionOptions): Promise<void> {
Expand Down Expand Up @@ -156,11 +158,20 @@ async function outputPages(
);
++context.counts.skipped_because_status;
} else {
if (options.requireSlugs && !page.hasExplicitSlug) {
error(
`Page "${page.nameOrTitle}" is missing a required slug. (--require-slugs is set.)`
);
++counts.error_because_no_slug;
}

const markdown = await getMarkdownForPage(config, context, page);
writePage(page, markdown);
}
}

if (counts.error_because_no_slug > 0) exit(1);

info(`Finished processing ${pages.length} pages`);
info(JSON.stringify(counts));
}
Expand Down
5 changes: 5 additions & 0 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ export async function run(): Promise<void> {
.option(
"-p, --img-prefix-in-markdown <string>",
"When referencing an image from markdown, prefix with this path instead of the full img-output-path. Should be used only in conjunction with --img-output-path."
)
.option(
"--require-slugs",
"If set, docu-notion will fail if any pages it would otherwise publish are missing a slug in Notion.",
false
);

program.showHelpAfterError();
Expand Down

0 comments on commit bff12fd

Please sign in to comment.