cremeaux/scripts/directus-to-markdown/index.js

110 lines
3.6 KiB
JavaScript
Raw Normal View History

2022-02-02 17:38:21 +01:00
import DirectusToMarkdown from '@resilien/directus-to-markdown'
import urlslug from 'url-slug'
2022-12-12 10:55:19 +01:00
const filterDraft = process.env.DRAFT && process.env.DRAFT == 'true' ? '' : { draft: { _eq: 'false' } }
const filterAssociation = process.env.DRAFT && process.env.DRAFT == 'true' ? '' : { display_website: { _eq: 'true' } }
2022-02-02 17:38:21 +01:00
const config = {
collections: {
2023-02-22 18:42:32 +01:00
Pages: {
readByQueryOption: {
2023-07-13 00:06:29 +02:00
fields: ['title', 'description', 'slug', 'image', 'image_credit', 'content', 'menu_display', 'sort', 'identifier', 'menu_title', 'parent', 'layout', 'aliases', 'draft'],
2023-02-22 18:42:32 +01:00
filterDraft
},
pathBuilder: (page) => {
page.path = page.parent ? 'index.md' : '_index.md'
if (!page.layout) delete page.layout
if (!page.image) delete page.image
if (!page.image_credit) delete page.image_credit
if (!page.aliases) delete page.aliases
2023-02-22 18:42:32 +01:00
if (page.menu_display) {
page.menu = {
main: {
name: page.menu_title,
weight: page.sort
}
}
if (page.identifier) {
page.menu.main.identifier = page.identifier
}
if (page.parent) {
page.menu.main.parent = page.parent
}
}
return `./content${page.slug}`;
},
2023-02-22 18:55:39 +01:00
deleteFields: ['path', 'slug', 'menu_display', 'sort', 'identifier', 'menu_title', 'parent'],
2023-02-22 18:42:32 +01:00
},
2022-02-02 17:38:21 +01:00
actualites: {
2022-02-23 11:43:36 +01:00
readByQueryOption: {
2023-06-06 16:54:45 +02:00
fields: ['title', 'date', 'image', 'image_credit', 'vignette', 'vignette_credit', 'description', 'auteur', 'draft', 'content', 'event'],
2023-12-04 16:01:32 +01:00
filterDraft,
limit: 1000
2022-02-02 17:38:21 +01:00
},
pathBuilder: (article) => {
2023-05-04 21:20:34 +02:00
if (!article.image) delete article.image
if (!article.image_credit) delete article.image_credit
if (!article.vignette) delete article.vignette
if (!article.vignette_credit) delete article.vignette_credit
2022-02-02 17:38:21 +01:00
const [year, month, day] = article.date.split("-")
return `./content/actualites/${year}/${month}/${day}-${urlslug(article.title, { remove: /\./g })}`;
2023-02-22 18:55:39 +01:00
},
deleteFields: [],
2022-12-02 08:54:45 +01:00
},
Association: {
readByQueryOption: {
fields: [
'denomination',
'title',
'description',
'mail',
'telephone',
'site',
'facebook',
'bureau.sort',
'bureau.Contact_id.*',
'content',
],
filterAssociation
},
pathBuilder: (association) => {
if (!association.title) {
association.title = association.denomination
}
console.log("Import de l'association « " + association.title + " »")
if (association.bureau && association.bureau.length > 0) {
const bureau = []
for (const contact of association.bureau) {
const detail = contact.Contact_id
if (detail) {
let c = {
denomination: detail.denomination,
prenom: detail.prenom,
nom: detail.nom,
}
if (detail.mail && detail.display_mail) {
c.mail = detail.mail
}
if (detail.telephone && detail.display_telephone) {
c.telephone = detail.telephone
}
bureau.push(c)
} else {
console.log("problème sur un contact")
}
}
association.bureau = bureau
} else {
delete association.bureau
}
2022-12-02 08:54:45 +01:00
return `./content/associations/${urlslug(association.denomination)}`;
},
deleteFields: [],
},
2022-02-02 17:38:21 +01:00
}
}
new DirectusToMarkdown(config).export();