2022-01-27 12:41:28 +01:00
|
|
|
# Directus To Markdown
|
|
|
|
|
|
|
|
This library export [Directus](https://directus.io) items collections to markdown files with assets.
|
|
|
|
|
|
|
|
I used it to export article from Directus to Hugo website.
|
|
|
|
|
|
|
|
## Configuration
|
|
|
|
|
|
|
|
### Directus
|
|
|
|
|
|
|
|
This library export data from an Directus so you should specify an url and token.
|
|
|
|
|
|
|
|
With environment variables:
|
|
|
|
|
|
|
|
```
|
|
|
|
export DIRECTUS_URL=https://your.directus.url
|
|
|
|
export DIRECTUS_TOKEN=your-token
|
|
|
|
```
|
|
|
|
|
2022-02-23 16:13:25 +01:00
|
|
|
or on configuration parameters:
|
2022-01-27 12:41:28 +01:00
|
|
|
|
|
|
|
```js
|
|
|
|
const config = {
|
|
|
|
url: 'https://your.directus.url',
|
|
|
|
token: 'your-token',
|
|
|
|
...
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### Collection Name
|
|
|
|
|
2022-02-23 16:13:25 +01:00
|
|
|
The key of collections object should be the name of Directus Collection:
|
2022-01-27 12:41:28 +01:00
|
|
|
|
|
|
|
```js
|
|
|
|
const config = {
|
|
|
|
collections: {
|
|
|
|
news: { ... },
|
|
|
|
pages: { ... }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### Content key
|
|
|
|
|
|
|
|
_default: content_
|
|
|
|
|
2022-02-23 16:13:25 +01:00
|
|
|
You can modify the field of the content:
|
2022-01-27 12:41:28 +01:00
|
|
|
|
|
|
|
```js
|
|
|
|
const config = {
|
|
|
|
contentKey: 'body',
|
|
|
|
...
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2022-02-23 16:13:25 +01:00
|
|
|
### deleteFields
|
|
|
|
|
|
|
|
Delete keys on markdown front matter:
|
|
|
|
|
|
|
|
```js
|
|
|
|
const config = {
|
|
|
|
deleteFields: ['id', 'jobs'],
|
|
|
|
...
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2022-02-21 17:33:50 +01:00
|
|
|
### readByQueryOption
|
2022-01-27 12:41:28 +01:00
|
|
|
|
2022-02-21 17:33:50 +01:00
|
|
|
`readByQueryOption` match https://docs.directus.io/reference/sdk/#read-by-query
|
2022-01-27 12:41:28 +01:00
|
|
|
|
|
|
|
### Export to specific path
|
|
|
|
|
|
|
|
For each collection you should an `pathBuilder`.
|
|
|
|
|
|
|
|
## Example
|
|
|
|
|
|
|
|
```js
|
|
|
|
import DirectusToMarkdown from '@resilien/directus-to-markdown'
|
|
|
|
import urlslug from 'url-slug'
|
|
|
|
|
|
|
|
const config = {
|
|
|
|
url: 'https://your.directus.url',
|
|
|
|
token: 'your-token',
|
|
|
|
contentKey: 'body',
|
|
|
|
collections: {
|
|
|
|
news: {
|
2022-02-21 17:33:50 +01:00
|
|
|
readByQueryOption: {
|
2022-01-27 12:41:28 +01:00
|
|
|
fields: ['title', 'slug', 'date', 'image', 'image_credit', 'draft', 'body'],
|
2022-02-21 17:33:50 +01:00
|
|
|
filter: { draft: { _eq: 'false' } },
|
|
|
|
limit: -1
|
2022-01-27 12:41:28 +01:00
|
|
|
},
|
|
|
|
pathBuilder: (article) => {
|
|
|
|
if (article.slug) {
|
|
|
|
return `./content/news/${article.slug}`
|
|
|
|
}
|
|
|
|
return `./content/news/${article.date}-${urlslug(article.title, { remove: /\./g })}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
new DirectusToMarkdown(config).export();
|
|
|
|
```
|