Compare commits

...

14 Commits
master ... main

Author SHA1 Message Date
Simon 869c4f82c1 Merge pull request 'feat: Download picture on array keys' (#5) from picture_download into main
## Détails

- permet de télécharger les images depuis directus qui sont contenu dans un array

## Pourquoi

Pour faciliter l'import d'images

Reviewed-on: https://git.weko.io/resilien/directus-to-markdown/pulls/5
2022-04-29 12:18:05 +02:00
Simon 4e11f4a3c7 feat: Download picture on array keys 2022-04-29 12:15:43 +02:00
Simon 7d96eb23a6 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
2022-02-23 16:18:11 +01:00
Simon 796a29b8fc chore: Upgrade version 2022-02-23 16:15:41 +01:00
Simon ab796326f1 feat: Update filename with path field 2022-02-23 16:14:51 +01:00
Simon 3af1ef11f7 feat: Add deleteFields field to delete keys on markdown front matter 2022-02-23 16:13:39 +01:00
Simon 111896770c Merge pull request 'chore: Upgrade to 1.0.1' (#3) from upgrade into main
Reviewed-on: https://git.weko.io/resilien/directus-to-markdown/pulls/3
2022-02-21 17:47:16 +01:00
Simon 20c4bfa09c chore: Upgrade to 1.0.1 2022-02-21 17:46:58 +01:00
Simon bed1ea7676 Merge pull request 'upgrade' (#2) from upgrade into main
## Détails

- Upgrade npm packages
- Fix breaking change on [9.5.2](https://github.com/directus/directus/releases/tag/v9.5.2)
- Upgrade package version

Reviewed-on: https://git.weko.io/resilien/directus-to-markdown/pulls/2
2022-02-21 17:35:52 +01:00
Simon afcace9358 chore: Upgrade version number 2022-02-21 17:34:29 +01:00
Simon adaba884fb fix: Upgrade readMany to readByQuery
It's a breaking change on 9.5.2 https://github.com/directus/directus/releases/tag/v9.5.2
2022-02-21 17:33:50 +01:00
Simon 9676d23fcc chore: Upgrade package 2022-02-21 17:21:55 +01:00
Simon e527a71708 Merge pull request 'feat: Add last newline on markdown file' (#1) from newlines into main
Reviewed-on: https://git.weko.io/resilien/directus-to-markdown/pulls/1
2022-01-28 00:18:39 +01:00
Simon 213fd6bf16 feat: Add last newline on markdown file 2022-01-28 00:18:00 +01:00
4 changed files with 2998 additions and 35 deletions

View File

@ -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,9 +53,20 @@ const config = {
}
```
### readManyOption
### deleteFields
`readManyOption` match https://docs.directus.io/reference/sdk/#read-multiple-items
Delete keys on markdown front matter:
```js
const config = {
deleteFields: ['id', 'jobs'],
...
}
```
### readByQueryOption
`readByQueryOption` match https://docs.directus.io/reference/sdk/#read-by-query
### Export to specific path
@ -73,9 +84,10 @@ const config = {
contentKey: 'body',
collections: {
news: {
readManyOption: {
readByQueryOption: {
fields: ['title', 'slug', 'date', 'image', 'image_credit', 'draft', 'body'],
filter: { draft: { _eq: 'false' } }
filter: { draft: { _eq: 'false' } },
limit: -1
},
pathBuilder: (article) => {
if (article.slug) {

View File

@ -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}`
const indexName = 'index' // TODO: index or _index ?
fs.writeFileSync(`${itemPath}/${indexName}.md`, itemContent)
const itemContent = `${frontMatter}${content}\r\n`
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)
@ -45,6 +53,10 @@ export default class DirectusToMarkdown {
if (typeof uuid === 'string' && uuid.match(uuidregex)) {
const filename = await this._downloadAsset(uuid, itemPath)
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)
}
}
}
}
@ -78,8 +90,9 @@ export default class DirectusToMarkdown {
async export() {
for (const collectionName in this.collections) {
const collection = this.collections[collectionName]
const readManyOption = collection.readManyOption
const items = (await this.directus.items(collectionName).readMany(readManyOption)).data
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)
if (!fs.existsSync(itemPath)) {
@ -87,7 +100,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)
}
}
}

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