summaryrefslogtreecommitdiff
path: root/src/build/index.mjs
blob: 74a7113b4e6a8438d61865beaba784fc9645ebf5 (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
import fs from 'node:fs';
import mustache from 'mustache';
import {u} from 'unist-builder';
import {h} from 'hastscript';
import {select} from 'hast-util-select';
import {toString as hastToString} from 'mdast-util-to-string';
import cssesc from 'cssesc';

import {toHtmlRaw, toString, toMdRaw, mdToHtmlRaw} from './to-html.mjs';
import loadSVG from './load-svg.mjs';
import listArticles from './list-articles.mjs';
import getRSS from './rss.mjs';
import toml from '@ltd/j-toml';

function getArticleYear(article) {
  if(article.metaData.pubDate.getFullYear) {
    return article.metaData.pubDate.getFullYear();
  } else if(article.metaData.pubDate.getUTCFullYear) {
    return article.metaData.pubDate.getUTCFullYear();
  }

  return 0;
}
function getArticleDate(article) {
  if(article.metaData.pubDate.getDate) {
    return article.metaData.pubDate.getFullYear() * 100 + article.metaData.pubDate.getDate();
  } else if(article.metaData.pubDate.getUTCDate) {
    return article.metaData.pubDate.getUTCFullYear() * 100 + article.metaData.pubDate.getDate();
  }

  return 0;
}
function cmpArticles(a, b) {
  return getArticleDate(b) - getArticleDate(a);
}

const loadMD = (listFile, suffix) => {
  const listContent = [];
  for (const file of listFile) {
    console.log(`Working on ${file}`);
    const content = fs.readFileSync(`${suffix}/${file}`, 'utf8');
    const mdRaw = toMdRaw(content);
    const tomlStringValue = mdRaw.children[0].value;
    const metaData = toml.parse(tomlStringValue);
    const newHTML = mdToHtmlRaw(mdRaw);

    const htmlContent = newHTML;
    const htmlRender = toString(htmlContent);

    const titleHtml = select('h1', htmlContent);
    const intro = select('p', htmlContent);
    intro.children = intro.children.filter(child => child.tagName !== 'br');

    const logo = select('img', intro);
    logo.properties.src = `${suffix}/${logo.properties.src}`;
    logo.properties.height = '150';
    logo.properties.width = '150';
    titleHtml.children[0].properties.href = `${suffix}/${file.slice(0, -3)}`;

    const title = hastToString(titleHtml);
    const domTitle = cssesc(title.replace(/\s+/g, '-').replace(/['"#@]/, '').toLowerCase()); // Maybe encodeURI

    const readMore = h('a', 'Lire plus...');

    readMore.properties.href = `${suffix}/${file.slice(0, -3)}`;
    const pubYear = getArticleYear({metaData});

    listContent.push({
      name: file.slice(0, -3),
      content: htmlRender,
      intro: toString(u('root', [titleHtml, intro, readMore])),
      introDesc: hastToString(intro),
      imageUrl: logo.properties.src,
      metaData,
      pubYear,
      title,
      domTitle,
      url: baseUrl + domTitle,
    });
  }

  return listContent;
};

const leftPanelTmpl = fs.readFileSync('src/templates/left.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 baseUrl = 'https://ache.one/';

const partials = {
  header: headerTmpl,
  leftPanel: leftPanelTmpl,
};

const svg = loadSVG();

const articles = loadMD(listArticles, 'articles');

const tagsArticle = new Map();

for (const article of articles) {
  const context = {
    svg,
    title: `${article.title} - ache`,
    canonical: article.url,
    content: article.content,
    domTitle: article.domTitle,
    metaData: article.metaData,
  };
  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]);
    }
  }
}

try {
  fs.mkdirSync('tag');
} catch {
  fs.rmSync('tag', {force: true, recursive: true});
  fs.mkdirSync('tag');
}

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

  const context = {
    svg,
    title: `ache - Tag: ${tag}`,
    tag,
    articles,
  };

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

console.log('Create RSS Flux: rss.xml');
const xmlFeed = getRSS(articles, baseUrl);
fs.writeFileSync('rss.xml', xmlFeed);

{
  const context = {
    title: 'ache: Blog personnel',
    canonical: baseUrl,
    svg,
    articles,
  };

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