summaryrefslogtreecommitdiff
path: root/src/build/index.mjs
blob: ad126a8abe87c726967d9a88e652bdd5d05ae99f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import process from 'node:process';
import fs from 'node:fs';
import mustache from 'mustache';
import loadSVG from './load-svg.mjs';
import getRSS from './rss.mjs';
import loadMD from './loadMD.mjs';
import {cmpArticles} from './article.mjs';
import i18n from './i18n.mjs';
import {addDescription} from './i18n.mjs';

const leftPanelTmpl = fs.readFileSync('src/templates/left.tmpl', 'utf8');
const likesTmpl = fs.readFileSync('src/templates/likes.tmpl', 'utf8');
const headerTmpl = fs.readFileSync('src/templates/header.tmpl', 'utf8');
const articleTmpl = fs.readFileSync('src/templates/article.tmpl', 'utf8');
const indexTmpl = fs.readFileSync('src/templates/index.tmpl', 'utf8');
const tagTmpl = fs.readFileSync('src/templates/tag.tmpl', 'utf8');
const hidTmpl = fs.readFileSync('src/templates/hid.tmpl', 'utf8');
const baseUrl = 'https://ache.one/';

const partials = {
  header: headerTmpl,
  leftPanel: leftPanelTmpl,
  likesButton: likesTmpl,
  hid: hidTmpl,
};

// Load global variables
const svg = loadSVG();

for (const lang in i18n) {
  const tagsArticle = new Map();
  const filter = process.argv.slice(2);
  const listArticle = (filter.length > 0) ? i18n[lang].articles.filter((article) => filter.includes(article.name)) : i18n[lang].articles;
  const articles = loadMD(listArticle, 'articles', lang);

  for (const article of articles) {
    const context = {
      svg,
      page_title: `${article.title} - ache`,
      title: i18n[lang].title,
      intro: i18n[lang].intro,
      lang,
      canonical: `${baseUrl}${article.url.slice(1)}`,
      content: article.content,
      title: i18n[lang].title,
      metaData: article.metaData,
      alt_lang: addDescription(article.metaData.alt_lang),
    };
    const output = mustache.render(articleTmpl, context, partials);

    console.log(`Create : ${article.title}`);
    fs.writeFileSync(`articles/${article.name}.html`, output);

    for (const tag of article.metaData.tags) {
      if (tagsArticle.has(tag)) {
        tagsArticle.get(tag).push(article);
      } else {
        tagsArticle.set(tag, [article]);
      }
    }
  }

// Set of pages language dependant

  try {
    fs.mkdirSync(`${lang}/tag`, {recursive: true});
  } catch {
    fs.rmSync(`${lang}/tag`, {force: true, recursive: true});
    fs.mkdirSync(`${lang}/tag`, {recursive: true});
  }

  for (const [tag, articles] of tagsArticle.entries()) {
    console.log(`Create tag page : ${lang}/${tag}.html`);
    articles.sort(cmpArticles);

    const context = {
      svg,
      page_title: `ache - Tag: ${tag}`,
      title: i18n[lang].title,
      intro: i18n[lang].intro,
      lang,
      tag,
      articles,
    };

    const output = mustache.render(tagTmpl, context, partials);
    fs.writeFileSync(`${lang}/tag/${tag}.html`, output);
  }

  console.log(`Create RSS Flux: ${lang}/rss.xml`);
  const xmlFeed = getRSS(articles, baseUrl, lang);
  fs.writeFileSync(`${lang}/rss.xml`, xmlFeed);

  {
    const alt_lang = {
      fr: {
        lang: 'en',
        url: '/en/',
      },
      en: {
        lang: 'fr',
        url: '/fr/',
      },
    };
    const context = {
      page_title: i18n[lang].title.main,
      title: i18n[lang].title,
      intro: i18n[lang].intro,
      lang,
      canonical: baseUrl,
      svg,
      articles,
      alt_lang: [alt_lang[lang]],
    };

    console.log(`Create : Home page ${lang}`);
    const output = mustache.render(indexTmpl, context, partials);
    fs.writeFileSync(`${lang}/index.html`, output);
  }
}