Merge pull request 'features' (#4) from features into main
## Détails - Ajout d'un champs deleteFields - Changement de nom de fichier markdown selon le champ `path` - Modification du numéro de version ## Pourquoi - Pour permettre de supprimer des champs dans le frontmatter des fichiers markdown - Pour permettre de changer facilement les noms des fichiers markdown - Pour créer une nouvelle release Reviewed-on: https://git.weko.io/resilien/directus-to-markdown/pulls/4
This commit is contained in:
commit
7d96eb23a6
11
README.md
11
README.md
|
@ -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
|
||||
|
|
25
index.js
25
index.js
|
@ -2,6 +2,7 @@ import { Directus } from '@directus/sdk'
|
|||
import yaml from 'js-yaml'
|
||||
import fs from 'fs'
|
||||
import Axios from 'axios'
|
||||
import path from 'path'
|
||||
|
||||
export default class DirectusToMarkdown {
|
||||
constructor(config) {
|
||||
|
@ -12,22 +13,29 @@ 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 ?
|
||||
fs.writeFileSync(`${itemPath}/${indexName}.md`, itemContent)
|
||||
const filePath = `${itemPath}/${item.path ? item.path : 'index.md'}`
|
||||
const fileDirname = path.dirname(filePath)
|
||||
if (!fs.existsSync(fileDirname)) {
|
||||
fs.mkdirSync(fileDirname, { recursive: true });
|
||||
}
|
||||
fs.writeFileSync(filePath, itemContent)
|
||||
}
|
||||
|
||||
async _writeFile(response, path) {
|
||||
const writer = fs.createWriteStream(path)
|
||||
async _writeFile(response, filePath) {
|
||||
const writer = fs.createWriteStream(filePath)
|
||||
|
||||
response.data.pipe(writer)
|
||||
|
||||
|
@ -79,6 +87,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 +96,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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
{
|
||||
"name": "@resilien/directus-to-markdown",
|
||||
"version": "1.0.1",
|
||||
"version": "1.1.0",
|
||||
"lockfileVersion": 2,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "@resilien/directus-to-markdown",
|
||||
"version": "1.0.1",
|
||||
"version": "1.1.0",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@directus/sdk": "^9.5.2",
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@resilien/directus-to-markdown",
|
||||
"version": "1.0.1",
|
||||
"version": "1.1.0",
|
||||
"description": "Export Directus items to markdown files with assets",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
|
|
Loading…
Reference in New Issue