feat: Add deleteFields field to delete keys on markdown front matter
This commit is contained in:
parent
111896770c
commit
3af1ef11f7
17
README.md
17
README.md
|
@ -17,7 +17,7 @@ export DIRECTUS_URL=https://your.directus.url
|
|||
export DIRECTUS_TOKEN=your-token
|
||||
```
|
||||
|
||||
or on configuration parameters :
|
||||
or on configuration parameters:
|
||||
|
||||
```js
|
||||
const config = {
|
||||
|
@ -29,7 +29,7 @@ const config = {
|
|||
|
||||
### Collection Name
|
||||
|
||||
The key of collections object should be the name of Directus Collection :
|
||||
The key of collections object should be the name of Directus Collection:
|
||||
|
||||
```js
|
||||
const config = {
|
||||
|
@ -44,7 +44,7 @@ const config = {
|
|||
|
||||
_default: content_
|
||||
|
||||
You can modify the field of the content :
|
||||
You can modify the field of the content:
|
||||
|
||||
```js
|
||||
const config = {
|
||||
|
@ -53,6 +53,17 @@ const config = {
|
|||
}
|
||||
```
|
||||
|
||||
### deleteFields
|
||||
|
||||
Delete keys on markdown front matter:
|
||||
|
||||
```js
|
||||
const config = {
|
||||
deleteFields: ['id', 'jobs'],
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
### readByQueryOption
|
||||
|
||||
`readByQueryOption` match https://docs.directus.io/reference/sdk/#read-by-query
|
||||
|
|
12
index.js
12
index.js
|
@ -12,14 +12,17 @@ export default class DirectusToMarkdown {
|
|||
this.directus = new Directus(this.url, { auth: { staticToken: this.token }});
|
||||
}
|
||||
|
||||
_formatFrontMatter(item) {
|
||||
_formatFrontMatter(item, deleteFields) {
|
||||
const front = { ...item } // copie item
|
||||
delete front[this.contentKey]
|
||||
for (const field of deleteFields) {
|
||||
delete front[field]
|
||||
}
|
||||
return `---\r\n${yaml.dump(front).trim()}\r\n---\r\n\r\n`
|
||||
}
|
||||
|
||||
async _writeIndex(item, itemPath) {
|
||||
const frontMatter = this._formatFrontMatter(item)
|
||||
async _writeIndex(item, itemPath, deleteFields) {
|
||||
const frontMatter = this._formatFrontMatter(item, deleteFields)
|
||||
const content = item[this.contentKey] ? item[this.contentKey].toString() : ''
|
||||
const itemContent = `${frontMatter}${content}\r\n`
|
||||
const indexName = 'index' // TODO: index or _index ?
|
||||
|
@ -79,6 +82,7 @@ export default class DirectusToMarkdown {
|
|||
for (const collectionName in this.collections) {
|
||||
const collection = this.collections[collectionName]
|
||||
const readByQueryOption = collection.readByQueryOption
|
||||
const deleteFields = collection.deleteFields
|
||||
const items = (await this.directus.items(collectionName).readByQuery(readByQueryOption)).data
|
||||
for (const item of items) {
|
||||
const itemPath = collection.pathBuilder(item)
|
||||
|
@ -87,7 +91,7 @@ export default class DirectusToMarkdown {
|
|||
}
|
||||
await this._downloadAssets(item, itemPath)
|
||||
await this._downloadAssetsFromContent(item, itemPath)
|
||||
await this._writeIndex(item, itemPath)
|
||||
await this._writeIndex(item, itemPath, deleteFields)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue