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

137 lines
4.7 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'
var lastDate = new Date();
lastDate.setMonth(lastDate.getMonth() - 3);
lastDate.setFullYear(lastDate.getFullYear() - 10);
console.log(lastDate)
const filterDraft = process.env.DRAFT && process.env.DRAFT == 'true' ? { date: { _gt: lastDate } } : { draft: { _eq: 'false' }, date: { _gt: lastDate } }
2022-12-12 10:55:19 +01:00
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: {
fields: ['title', 'date', 'image', 'image_credit', 'vignette', 'vignette_credit', 'description', 'auteur', 'draft', 'content', 'event', 'authors.Association_id.denomination'],
filter: filterDraft,
2023-12-04 16:01:32 +01:00
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
if (!article.auteur) delete article.auteur
if (!article.authors || article.authors.length == 0) {
delete article.authors
} else {
article.authors = article.authors.map(a => `/associations/${urlslug(a.Association_id.denomination)}`)
}
2022-02-02 17:38:21 +01:00
const [year, month, day] = article.date.split("-")
console.log(article.date + ' - ' + article.title)
2022-02-02 17:38:21 +01:00
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',
'actualites.actualites_id.date',
'actualites.actualites_id.title',
],
filter: 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
if (!association.actualites || association.actualites.length == 0) {
delete association.actualites
} else {
association.actualites = association.actualites.map(a => {
const [year, month, day] = a.actualites_id.date.split("-")
return `/actualites/${year}/${month}/${day}-${urlslug(a.actualites_id.title, { remove: /\./g })}`
})
}
return `./content/associations/${urlslug(association.denomination)}`;
},
deleteFields: [],
},
2022-02-02 17:38:21 +01:00
}
}
new DirectusToMarkdown(config).export();