Skip to content

Commit

Permalink
1. add support for slug.
Browse files Browse the repository at this point in the history
2. change the default value of timezone.
  • Loading branch information
Doradx committed Sep 7, 2023
1 parent 589037f commit 746d26a
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 20 deletions.
13 changes: 9 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ inputs:
- 那么 `database_id=0f3d856498ca4db3b457c5b4eeaxxxxx`
status_name:
required: false
description: notion database 状态字段的字段名, 支持自定义
default: "status"
description: notion database 状态字段的字段名, 默认为pstatus, 可修改
default: "pstatus"
status_published:
required: false
description: notion database 文章已发布状态的字段值
Expand All @@ -36,7 +36,7 @@ inputs:
description: |
转换后markdown文件中需要保留的字段, 字段值会同步到Notion中。
例如abbrlink等字段, 在notion->markdown过程不存在, 但需要hexo等博客自动生成, 且不能变化。
多个key请用逗号分隔, 如abbrlink, id.
多个key请用逗号分隔, 如abbrlink, id
default: "abbrlink"
last_sync_datetime:
required: false
Expand Down Expand Up @@ -85,8 +85,13 @@ inputs:
default: "{}"
pic_compress:
description: |
是否开启图片压缩true为开启, 默认不开启
是否开启图片压缩? true为开启, 默认不开启
default: "false"
excluded_metas:
default: "ptype, pstatus"
description: |
Notion转Markdown, 生成页面YAML是需要删除的属性名称, 多个用逗号分隔
需要删除的页面属性名称
timezone:
description: 设置的时区
default: "Asia/Shanghai"
Expand Down
33 changes: 25 additions & 8 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -282381,8 +282381,8 @@ let notion = new Client({ auth: config.notion_secret });
let picgo = new PicGo();
let n2m = new NotionToMarkdown({ notionClient: notion });

function init(conf) {
config = conf;
function init(cfg) {
config = cfg;
notion = new Client({
auth: config.notion_secret,
config: {
Expand Down Expand Up @@ -282436,18 +282436,18 @@ async function sync() {
// query the filename list from the output directory
let notionPagePropList = await Promise.all(pages.map(async (page) => {
var properties = await getPropertiesDict(page);
switch (properties.type) {
switch (properties?.ptype?.toLocaleLowerCase() || "") {
case "page":
if (!properties.filename) {
if (!properties?.filename && !properties?.slug) {
console.error(`Page ${properties.title} has no filename, the page id will be used as the filename.`);
properties.filename = properties.id;
}
properties.filePath = path.join(config.output_dir.page, properties.filename, 'index.md');
properties.filePath = path.join(config.output_dir.page, (properties?.filename || properties?.slug).trim(), 'index.md');
properties.filename = "index.md";
break;
case "post":
default:
properties.filename = properties.filename != undefined && properties.filename ? properties.filename + ".md" : properties.title + ".md";
properties.filename = (properties?.filename || properties?.slug || properties?.title || properties.id).trim() + '.md'
// get the filename and directory of the post, if the filename includes /, then it will be treated as a subdirectory
properties.filePath = path.join(config.output_dir.post, properties.filename);
if (properties.filename.includes("/")) {
Expand Down Expand Up @@ -282620,6 +282620,14 @@ async function page2Markdown(page, filePath, properties) {
}
}
// remove created_time and last_edited_time from properties
if (config?.excluded_metas && config.excluded_metas.length){
// delete the key within excluded_metas for properties
for(const key of config.excluded_metas){
if(key && key in properties) {
delete properties[key];
}
}
}
delete properties.created_time;
delete properties.last_edited_time;
let fm = YAML.stringify(properties, { doubleQuotedAsJSON: true });
Expand Down Expand Up @@ -282786,7 +282794,7 @@ function getPropVal(data) {
case "date":
var mt = moment(val.start);
if (!mt.isValid()) return val.start;
return mt.tz(config.timezone).format('YYYY-MM-DD HH:mm:ss');
return config?.timezone ? mt.tz(config.timezone).format('YYYY-MM-DD') : mt.format();
case "rich_text":
case "title":
return val.map((a) => a.plain_text).join("");
Expand All @@ -282799,7 +282807,7 @@ function getPropVal(data) {
case "last_edited_time":
var mt = moment(val);
if (!mt.isValid()) return val;
return mt.tz(config.timezone).format('YYYY-MM-DD HH:mm:ss');
return config?.timezone ? mt.tz(config.timezone).format('YYYY-MM-DD HH:mm:ss') : mt.format();
default:
return "";
}
Expand Down Expand Up @@ -305415,6 +305423,14 @@ if (keys_to_keep && keys_to_keep.trim().length > 0) {
keys_to_keep = keys_to_keep.split(",").map((key) => key.trim());
}

var excluded_metas = core.getInput("excluded_metas") || [];
if(excluded_metas){
excluded_metas = excluded_metas.split(',');
// use trim to remove space for excluded_metas;
excluded_metas = excluded_metas.forEach((v)=>v.trim());
excluded_metas = excluded_metas.filter((v)=>v);
}

let config = {
notion_secret: core.getInput("notion_secret"),
database_id: core.getInput("database_id"),
Expand All @@ -305432,6 +305448,7 @@ let config = {
},
keys_to_keep: keys_to_keep,
last_sync_datetime: core.getInput("last_sync_datetime") || null,
excluded_metas: excluded_metas || [],
timezone: core.getInput("timezone") || "Asia/Shanghai",
};

Expand Down
Binary file removed dist/vendor1/pngquant
Binary file not shown.
Binary file removed dist/vendor1/source/pngquant.tar.gz
Binary file not shown.
Binary file removed dist/vendor2/cjpeg
Binary file not shown.
Binary file removed dist/vendor2/source/mozjpeg.tar.gz
Binary file not shown.
9 changes: 9 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ if (keys_to_keep && keys_to_keep.trim().length > 0) {
keys_to_keep = keys_to_keep.split(",").map((key) => key.trim());
}

var excluded_metas = core.getInput("excluded_metas") || [];
if(excluded_metas){
excluded_metas = excluded_metas.split(',');
// use trim to remove space for excluded_metas;
excluded_metas = excluded_metas.forEach((v)=>v.trim());
excluded_metas = excluded_metas.filter((v)=>v);
}

let config = {
notion_secret: core.getInput("notion_secret"),
database_id: core.getInput("database_id"),
Expand All @@ -57,6 +65,7 @@ let config = {
},
keys_to_keep: keys_to_keep,
last_sync_datetime: core.getInput("last_sync_datetime") || null,
excluded_metas: excluded_metas || [],
timezone: core.getInput("timezone") || "Asia/Shanghai",
};

Expand Down
24 changes: 16 additions & 8 deletions src/notion.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ let notion = new Client({ auth: config.notion_secret });
let picgo = new PicGo();
let n2m = new NotionToMarkdown({ notionClient: notion });

function init(conf) {
config = conf;
function init(cfg) {
config = cfg;
notion = new Client({
auth: config.notion_secret,
config: {
Expand Down Expand Up @@ -102,18 +102,18 @@ async function sync() {
// query the filename list from the output directory
let notionPagePropList = await Promise.all(pages.map(async (page) => {
var properties = await getPropertiesDict(page);
switch (properties.type) {
switch (properties?.ptype?.toLocaleLowerCase() || "") {
case "page":
if (!properties.filename) {
if (!properties?.filename && !properties?.slug) {
console.error(`Page ${properties.title} has no filename, the page id will be used as the filename.`);
properties.filename = properties.id;
}
properties.filePath = path.join(config.output_dir.page, properties.filename, 'index.md');
properties.filePath = path.join(config.output_dir.page, (properties?.filename || properties?.slug).trim(), 'index.md');
properties.filename = "index.md";
break;
case "post":
default:
properties.filename = properties.filename != undefined && properties.filename ? properties.filename + ".md" : properties.title + ".md";
properties.filename = (properties?.filename || properties?.slug || properties?.title || properties.id).trim() + '.md'
// get the filename and directory of the post, if the filename includes /, then it will be treated as a subdirectory
properties.filePath = path.join(config.output_dir.post, properties.filename);
if (properties.filename.includes("/")) {
Expand Down Expand Up @@ -286,6 +286,14 @@ async function page2Markdown(page, filePath, properties) {
}
}
// remove created_time and last_edited_time from properties
if (config?.excluded_metas && config.excluded_metas.length){
// delete the key within excluded_metas for properties
for(const key of config.excluded_metas){
if(key && key in properties) {
delete properties[key];
}
}
}
delete properties.created_time;
delete properties.last_edited_time;
let fm = YAML.stringify(properties, { doubleQuotedAsJSON: true });
Expand Down Expand Up @@ -452,7 +460,7 @@ function getPropVal(data) {
case "date":
var mt = moment(val.start);
if (!mt.isValid()) return val.start;
return mt.tz(config.timezone).format('YYYY-MM-DD HH:mm:ss');
return config?.timezone ? mt.tz(config.timezone).format('YYYY-MM-DD') : mt.format();
case "rich_text":
case "title":
return val.map((a) => a.plain_text).join("");
Expand All @@ -465,7 +473,7 @@ function getPropVal(data) {
case "last_edited_time":
var mt = moment(val);
if (!mt.isValid()) return val;
return mt.tz(config.timezone).format('YYYY-MM-DD HH:mm:ss');
return config?.timezone ? mt.tz(config.timezone).format('YYYY-MM-DD HH:mm:ss') : mt.format();
default:
return "";
}
Expand Down

0 comments on commit 746d26a

Please sign in to comment.