Compare commits

..

No commits in common. "main" and "master" have entirely different histories.
main ... master

4 changed files with 35 additions and 2998 deletions

View File

@ -17,7 +17,7 @@ export DIRECTUS_URL=https://your.directus.url
export DIRECTUS_TOKEN=your-token export DIRECTUS_TOKEN=your-token
``` ```
or on configuration parameters: or on configuration parameters :
```js ```js
const config = { const config = {
@ -29,7 +29,7 @@ const config = {
### Collection Name ### 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 ```js
const config = { const config = {
@ -44,7 +44,7 @@ const config = {
_default: content_ _default: content_
You can modify the field of the content: You can modify the field of the content :
```js ```js
const config = { const config = {
@ -53,20 +53,9 @@ const config = {
} }
``` ```
### deleteFields ### readManyOption
Delete keys on markdown front matter: `readManyOption` match https://docs.directus.io/reference/sdk/#read-multiple-items
```js
const config = {
deleteFields: ['id', 'jobs'],
...
}
```
### readByQueryOption
`readByQueryOption` match https://docs.directus.io/reference/sdk/#read-by-query
### Export to specific path ### Export to specific path
@ -84,10 +73,9 @@ const config = {
contentKey: 'body', contentKey: 'body',
collections: { collections: {
news: { news: {
readByQueryOption: { readManyOption: {
fields: ['title', 'slug', 'date', 'image', 'image_credit', 'draft', 'body'], fields: ['title', 'slug', 'date', 'image', 'image_credit', 'draft', 'body'],
filter: { draft: { _eq: 'false' } }, filter: { draft: { _eq: 'false' } }
limit: -1
}, },
pathBuilder: (article) => { pathBuilder: (article) => {
if (article.slug) { if (article.slug) {

View File

@ -2,7 +2,6 @@ import { Directus } from '@directus/sdk'
import yaml from 'js-yaml' import yaml from 'js-yaml'
import fs from 'fs' import fs from 'fs'
import Axios from 'axios' import Axios from 'axios'
import path from 'path'
export default class DirectusToMarkdown { export default class DirectusToMarkdown {
constructor(config) { constructor(config) {
@ -13,29 +12,22 @@ export default class DirectusToMarkdown {
this.directus = new Directus(this.url, { auth: { staticToken: this.token }}); this.directus = new Directus(this.url, { auth: { staticToken: this.token }});
} }
_formatFrontMatter(item, deleteFields) { _formatFrontMatter(item) {
const front = { ...item } // copie item const front = { ...item } // copie item
delete front[this.contentKey] 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` return `---\r\n${yaml.dump(front).trim()}\r\n---\r\n\r\n`
} }
async _writeIndex(item, itemPath, deleteFields) { async _writeIndex(item, itemPath) {
const frontMatter = this._formatFrontMatter(item, deleteFields) const frontMatter = this._formatFrontMatter(item)
const content = item[this.contentKey] ? item[this.contentKey].toString() : '' const content = item[this.contentKey] ? item[this.contentKey].toString() : ''
const itemContent = `${frontMatter}${content}\r\n` const itemContent = `${frontMatter}${content}`
const filePath = `${itemPath}/${item.path ? item.path : 'index.md'}` const indexName = 'index' // TODO: index or _index ?
const fileDirname = path.dirname(filePath) fs.writeFileSync(`${itemPath}/${indexName}.md`, itemContent)
if (!fs.existsSync(fileDirname)) {
fs.mkdirSync(fileDirname, { recursive: true });
}
fs.writeFileSync(filePath, itemContent)
} }
async _writeFile(response, filePath) { async _writeFile(response, path) {
const writer = fs.createWriteStream(filePath) const writer = fs.createWriteStream(path)
response.data.pipe(writer) response.data.pipe(writer)
@ -53,10 +45,6 @@ export default class DirectusToMarkdown {
if (typeof uuid === 'string' && uuid.match(uuidregex)) { if (typeof uuid === 'string' && uuid.match(uuidregex)) {
const filename = await this._downloadAsset(uuid, itemPath) const filename = await this._downloadAsset(uuid, itemPath)
item[key] = filename // Update field with filename instead of uuid item[key] = filename // Update field with filename instead of uuid
} else if (Array.isArray(uuid)) {
for (const element of uuid) {
await this._downloadAssets(element, itemPath)
}
} }
} }
} }
@ -90,9 +78,8 @@ export default class DirectusToMarkdown {
async export() { async export() {
for (const collectionName in this.collections) { for (const collectionName in this.collections) {
const collection = this.collections[collectionName] const collection = this.collections[collectionName]
const readByQueryOption = collection.readByQueryOption const readManyOption = collection.readManyOption
const deleteFields = collection.deleteFields const items = (await this.directus.items(collectionName).readMany(readManyOption)).data
const items = (await this.directus.items(collectionName).readByQuery(readByQueryOption)).data
for (const item of items) { for (const item of items) {
const itemPath = collection.pathBuilder(item) const itemPath = collection.pathBuilder(item)
if (!fs.existsSync(itemPath)) { if (!fs.existsSync(itemPath)) {
@ -100,7 +87,7 @@ export default class DirectusToMarkdown {
} }
await this._downloadAssets(item, itemPath) await this._downloadAssets(item, itemPath)
await this._downloadAssetsFromContent(item, itemPath) await this._downloadAssetsFromContent(item, itemPath)
await this._writeIndex(item, itemPath, deleteFields) await this._writeIndex(item, itemPath)
} }
} }
} }

2968
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{ {
"name": "@resilien/directus-to-markdown", "name": "@resilien/directus-to-markdown",
"version": "1.2.0", "version": "0.1.0",
"description": "Export Directus items to markdown files with assets", "description": "Export Directus items to markdown files with assets",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
@ -21,7 +21,7 @@
"license": "ISC", "license": "ISC",
"type": "module", "type": "module",
"dependencies": { "dependencies": {
"@directus/sdk": "^9.9.1", "@directus/sdk": "^9.5.0",
"js-yaml": "^4.1.0" "js-yaml": "^4.1.0"
} }
} }