📝 doc: add developer guide document

This commit is contained in:
frostime 2024-03-16 13:43:27 +08:00
parent 0ff9fde560
commit 4a7bd848a9
2 changed files with 107 additions and 0 deletions

View file

@ -224,3 +224,57 @@ PR 社区集市仓库。
- 第二十行: `svelte(),`
4. 删掉 `tsconfig.json` 中 37 行 `"svelte"`
5. 重新执行 `pnpm i`
## 开发者须知
思源开发者需注意以下规范。
### 1. 读写文件规范
插件或者外部扩展如果有直接读取或者写入 data 下文件的需求,请通过调用内核 API 来实现,**不要自行调用 `fs` 或者其他 electron、nodejs API**,否则可能会导致数据同步时分块丢失,造成云端数据损坏。
相关 API 见: `/api/file/*`(例如 `/api/file/getFile` 等)。
### 2. Daily Note 属性规范
思源在创建日记的时候会自动为文档添加 custom-dailynote-yyyymmdd 属性, 以方便将日记文档同普通文档区分。
> 详情请见 [Github Issue #9807](https://github.com/siyuan-note/siyuan/issues/9807)。
开发者在开发手动创建 Daily Note 的功能时请注意:
- 如果调用了 `/api/filetree/createDailyNote` 创建日记,那么文档会自动添加这个属性,无需开发者特别处理。
- 如果是开发者代码手动创建文档(例如使用 `createDocWithMd` API 创建日记),请手动为文档添加该属性。
参考代码:
```ts
/*
* Copyright (c) 2023 by frostime. All Rights Reserved.
* @Author : frostime
* @Url : https://github.com/frostime/siyuan-dailynote-today/blob/v1.3.0/src/func/dailynote/dn-attr.ts
*/
export function formatDate(date?: Date, sep=''): string {
date = date === undefined ? new Date() : date;
let year = date.getFullYear();
let month = date.getMonth() + 1;
let day = date.getDate();
return `${year}${sep}${month < 10 ? '0' + month : month}${sep}${day < 10 ? '0' + day : day}`;
}
/**
* Set custom attribute: `custom-dailynote-yyyyMMdd`: yyyyMMdd
* https://github.com/siyuan-note/siyuan/issues/9807
* @param doc_id Id of daily note
*/
export function setCustomDNAttr(doc_id: string, date?: Date) {
let td = formatDate(date);
let attr = `custom-dailynote-${td}`;
// 构建 attr: td
let attrs: { [key: string]: string } = {};
attrs[attr] = td;
serverApi.setBlockAttrs(doc_id, attrs);
}
```