From 3af1ef11f75d764fa1464636af11386e618be055 Mon Sep 17 00:00:00 2001 From: Simon C Date: Wed, 23 Feb 2022 16:13:25 +0100 Subject: [PATCH 1/3] feat: Add deleteFields field to delete keys on markdown front matter --- README.md | 17 ++++++++++++++--- index.js | 12 ++++++++---- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 3f6e1b9..a070238 100644 --- a/README.md +++ b/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 diff --git a/index.js b/index.js index 4526133..73b944d 100644 --- a/index.js +++ b/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) } } } From ab796326f11f6c8424faab4824e2dc764ddb5b97 Mon Sep 17 00:00:00 2001 From: Simon C Date: Wed, 23 Feb 2022 16:14:51 +0100 Subject: [PATCH 2/3] feat: Update filename with path field --- index.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 73b944d..5c0489d 100644 --- a/index.js +++ b/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) { @@ -25,12 +26,16 @@ export default class DirectusToMarkdown { 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) From 796a29b8fcdce8b212b37b6a11cfc76291be7f19 Mon Sep 17 00:00:00 2001 From: Simon C Date: Wed, 23 Feb 2022 16:15:41 +0100 Subject: [PATCH 3/3] chore: Upgrade version --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3f5e5be..d9d9a10 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 8b5a67c..dba72a9 100644 --- a/package.json +++ b/package.json @@ -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": {