summaryrefslogtreecommitdiff
path: root/src/build/index.mjs
blob: d28f8c4846ff5b6dfd5bdac519aaf7f660f7015b (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
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";
import { minifyHTML } from "./utils.mjs";

import { SitemapStream, streamToPromise } from "sitemap";
import { Readable } from "stream";

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 notesTmpl = fs.readFileSync("src/templates/notes.tmpl", "utf8");
const legalTmpl = fs.readFileSync("src/templates/legal.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();

let links = [];

const listNotes = fs
  .readdirSync("notes")
  .filter((name) => name.endsWith(".md"));
const notesAll = loadMD(listNotes, "notes"); // lang is determined from the note
const legalPages = loadMD(["legal.md", "legal-en.md"], "src/pages");

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

  // Same for notes
  const notes = notesAll.filter((note) => note.metaData.lang == lang);
  const material = [...articles, ...notes];

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

  for (const post of material) {
    const context = {
      svg,
      page_title: `${post.title} - ache`,
      title: i18n[lang].title,
      intro: i18n[lang].intro,
      lang,
      canonical: `${baseUrl}${post.url.slice(1)}`,
      content: post.content,
      title: i18n[lang].title,
      metaData: post.metaData,
      alt_lang: addDescription(post.metaData.alt_lang),
      description: post.intro,

      like_title: i18n[lang].like_title,
      like_text: i18n[lang].like_text,
    };
    const output = mustache.render(articleTmpl, context, partials);

    console.log(`Create : ${post.title}`);
    const type = post.metaData?.type || "article";
    fs.writeFileSync(`${type}s/${post.name}.html`, minifyHTML(output));
    links.push({
      url: context.canonical,
      changefreq: "yearly",
      priority: 0.6,
      img: [{ url: post?.imageUrl }],
    });

    for (const tag of post.metaData.tags) {
      // Insert the tag, if it already exists add it, otherwise create a object for it.
      if (tagsArticle.has(tag)) {
        tagsArticle.get(tag).push(post);
      } else {
        tagsArticle.set(tag, [post]);
      }
    }
  }

  // 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, material] of tagsArticle.entries()) {
    console.log(`Create tag page : ${lang}/${tag}.html`);
    material.sort(cmpArticles);

    const articles = material.filter((item) => item.metaData.type != "note");
    const notes = material.filter((item) => item.metaData.type == "note");

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

    const output = mustache.render(tagTmpl, context, partials);
    fs.writeFileSync(`${lang}/tag/${tag}.html`, minifyHTML(output));
    links.push({
      url: `${baseUrl}${lang}/tag/${tag}`,
      changefreq: "monthly",
      priority: 0.3,
    });
  }

  {
    console.log(`Create RSS Flux: ${lang}/rss.xml`);
    const xmlFeed = getRSS(articles, baseUrl, lang);
    fs.writeFileSync(`${lang}/rss.xml`, xmlFeed);
    links.push({
      url: `${baseUrl}${lang}/rss.xml`,
      changefreq: "monthly",
      priority: 0.3,
    });
  }

  {
    console.log(`Create RSS Flux: ${lang}/rss-full.xml`);
    const xmlFeed = getRSS(material, baseUrl, lang, "rss_full");
    fs.writeFileSync(`${lang}/rss-full.xml`, xmlFeed);
    links.push({
      url: `${baseUrl}${lang}/rss-full.xml`,
      changefreq: "monthly",
      priority: 0.3,
    });
  }

  // Notes page: A note with a list of every note in that language
  {
    // Pages to redirect to when switching languages.
    const alt_lang = {
      fr: {
        lang: "en",
        url: "/en/notes",
      },
      en: {
        lang: "fr",
        url: "/fr/notes",
      },
    };
    const context = {
      page_title: i18n[lang].title.main,
      title: i18n[lang].title,
      intro: i18n[lang].intro,
      lang,
      canonical: `${baseUrl}${lang}`,
      svg,
      notes,
      alt_lang: [alt_lang[lang]],
      description: i18n[lang].index_desc,
    };

    console.log(`Create : Notes page ${lang}`);
    const output = mustache.render(notesTmpl, context, partials);
    fs.writeFileSync(`${lang}/notes/index.html`, minifyHTML(output));
    links.push({
      url: `${baseUrl}${lang}/notes`,
      changefreq: "monthly",
      priority: 0.7,
      img: [{ url: "https://ache.one/res/ache.svg" }],
    });
  }

  // Legal page
  {
    const legal_page = legalPages.filter(
      (page) => page.metaData.lang == lang,
    )[0];
    // Pages to redirect to when switching languages.
    const alt_lang = {
      fr: {
        lang: "en",
        url: "/en/legal",
      },
      en: {
        lang: "fr",
        url: "/fr/legal",
      },
    };
    const context = {
      page_title: i18n[lang].title.legal,
      title: i18n[lang].title,
      intro: i18n[lang].intro,
      lang,
      canonical: `${baseUrl}${lang}/legal`,
      svg,
      content: legal_page.content,
      alt_lang: [alt_lang[lang]],
      description: i18n[lang].index_desc,
    };

    console.log(`Create : Legal page ${lang}`);
    const output = mustache.render(legalTmpl, context, partials);

    fs.writeFileSync(`${lang}/legal.html`, minifyHTML(output));
    links.push({
      url: `${lang}/legal.html`,
      changefreq: "yearly",
      priority: 0.1,
    });
  }

  // Home page
  {
    // Pages to redirect to when switching languages.
    const alt_lang = {
      fr: {
        lang: "en",
        url: "/en/",
      },
      en: {
        lang: "fr",
        url: "/fr/",
      },
    };
    const context = {
      page_title: i18n[lang].title.notes,
      title: i18n[lang].title,
      intro: i18n[lang].intro,
      lang,
      canonical: `${baseUrl}${lang}/notes`,
      svg,
      articles,
      alt_lang: [alt_lang[lang]],
      description: i18n[lang].index_desc,
    };

    console.log(`Create : Home page ${lang}`);
    const output = mustache.render(indexTmpl, context, partials);
    fs.writeFileSync(`${lang}/index.html`, minifyHTML(output));
    links.push({
      url: `${baseUrl}${lang}`,
      changefreq: "monthly",
      priority: 0.7,
      img: [{ url: "https://ache.one/res/ache.svg" }],
    });
  }
}

console.log(`Create: sitemap.xml`);
// Create a stream to write to
const stream = new SitemapStream({ hostname: "https://ache.one" });

// Return a promise that resolves with your XML string
streamToPromise(Readable.from(links).pipe(stream))
  .then((data) => data.toString())
  .then((sitemapXML) => fs.writeFileSync("sitemap.xml", sitemapXML));