Skip to content
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

Using specified property for markdown filename #50

Merged
merged 4 commits into from
Feb 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ name: 'build-test'

on: # rebuild any PRs and main branch changes
pull_request:
push:
branches:
- main
- 'v*'

jobs:
build: # make sure build/ci work properly
runs-on: ubuntu-latest
timeout-minutes: 3
steps:
- uses: actions/checkout@v3
with:
ref: ${{ github.head_ref }}

- name: Set Node.js 16.x
uses: actions/setup-node@v3
Expand All @@ -29,9 +27,25 @@ jobs:
runs-on: ubuntu-latest
timeout-minutes: 3
steps:
- uses: actions/checkout@v3
- name: Checkout source
uses: actions/checkout@v3
with:
ref: ${{ github.head_ref }}

- name: Normal test
uses: ./
env:
NOTION_API_KEY: ${{ secrets.NOTION_API_KEY }}
NOTION_DATABASE_ID: ${{ secrets.NOTION_DATABASE_ID }}
ACTIONS_STEP_DEBUG: true
ACTIONS_RUNNER_DEBUG: true

- uses: ./
- name: Specific property as a markdown filename
uses: ./
with:
filename_property: 'slug'
env:
NOTION_API_KEY: ${{ secrets.NOTION_API_KEY }}
NOTION_DATABASE_ID: ${{ secrets.NOTION_DATABASE_ID }}
ACTIONS_STEP_DEBUG: true
ACTIONS_RUNNER_DEBUG: true
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ This action converts and downloads pages that exist in Notion's specified databa

## Usage

`.github/workflows/download.yml`
`.github/workflows/import.yml`

```yaml
name: download
name: import

on:
schedule:
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ inputs:
required: false
description: 'Directory path to output files.'
default: 'output'
filename_property:
required: false
description: 'A property to be used as the markdown file name.'
default: 'title'
runs:
using: 'node16'
main: 'dist/index.js'
Expand Down
34 changes: 28 additions & 6 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "notion-to-markdown-action",
"version": "0.1.5",
"version": "0.2.1",
"private": true,
"description": "Convert notion pages to markdown files action",
"main": "lib/main.js",
"scripts": {
"build": "tsc",
"build:watch": "tsc -w",
"start": "node ./dist/index.js",
"start": "ACTIONS_RUNNER_DEBUG=true ACTIONS_STEP_DEBUG=true node ./dist/index.js",
"format": "prettier --write '**/*.ts'",
"format-check": "prettier --check '**/*.ts'",
"lint": "eslint src/**/*.ts",
Expand Down
26 changes: 21 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@ import {
findImagesFromMarkdown,
MarkdownPage
} from './utils/markdown';

const DEFAULT_OUTPUT = 'output';
import {
DEFAULT_FILENAME_PROPERTY,
DEFAULT_OUTPUT_DIR
} from './utils/constants';

const auth = process.env.NOTION_API_KEY as string;
const databaseId = process.env.NOTION_DATABASE_ID as string;

const run = async (auth: string, databaseId: string, outDir: string) => {
const run = async (
auth: string,
databaseId: string,
outDir: string,
filenameProperty: string
) => {
const client = new Client({ auth });

info('Call "Query a Database" API ...');
Expand All @@ -25,7 +32,11 @@ const run = async (auth: string, databaseId: string, outDir: string) => {

info('Convert notion pages to markdown files ...');

const mdResponse = await convertPagesToMarkdown(client, pages);
const mdResponse = await convertPagesToMarkdown(
client,
pages,
filenameProperty
);

info('---> Successfully converted from notion pages to markdown files!');

Expand Down Expand Up @@ -71,7 +82,12 @@ const downloadImages = async (filename: string, outDir: string) => {
}
};

run(auth, databaseId, getInput('output_path') || DEFAULT_OUTPUT).catch((e) => {
run(
auth,
databaseId,
getInput('output_path') || DEFAULT_OUTPUT_DIR,
getInput('filename_property') || DEFAULT_FILENAME_PROPERTY
).catch((e) => {
error(e);
setFailed(e.message);
});
2 changes: 2 additions & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const DEFAULT_OUTPUT_DIR = 'output';
export const DEFAULT_FILENAME_PROPERTY = 'title';
16 changes: 14 additions & 2 deletions src/utils/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ export const convertPagesToMarkdown = async (
notionClient: Client,
pages: (PageObjectResponse & {
frontmatter: Frontmatter;
})[]
})[],
prop: string
): Promise<MarkdownPage[]> => {
const n2m = new NotionToMarkdown({ notionClient });
return Promise.all(
pages.map(async (page) => {
const frontmatter = renderedMatter(page.frontmatter);
const markdown = await pageToMarkdown(n2m, page.id);
const filename = slug(detectFilenameFromProperty(page.frontmatter, prop));
return {
filename: slug(page.frontmatter.title),
filename,
body: format([frontmatter, markdown].join('\n'), { parser: 'markdown' })
};
})
Expand Down Expand Up @@ -74,3 +76,13 @@ export const findImagesFromMarkdown = async (filename: string) => {
}
return { images, content };
};

const detectFilenameFromProperty = (frontmatter: Frontmatter, prop: string) => {
if (prop === 'title' || !(prop in frontmatter)) {
return frontmatter.title;
}

return typeof frontmatter[prop] === 'string'
? (frontmatter[prop] as string)
: frontmatter.title;
};