summaryrefslogtreecommitdiff
path: root/_posts/processPosts.js
blob: d4b2a2e2436c0802cc19bfd0c88512d83c2ff4d8 (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


const fs = require('fs');
const path = require('path');

const tags = [
  {
    id: 'personal',
    title: 'Personal 👨'
  }
]

const posts = [
  {
    url: "_posts/hello_world.html",
    title: "Hello, World!",
    tags: [ "personal" ]
  }
];

function createTagFile(tag) {
  const dir = path.resolve(path.join(__dirname, '..', 'posts'));

  if (!fs.existsSync(dir)) {
    fs.mkdirSync(dir);
  }


  const tagFileName = `tag_${tag.id}.html`
  createPostPage(path.join(__dirname, '..', 'posts', tagFileName), tag.id)
  return `/posts/${tagFileName}`;
}

function createTag(tag) {
  return `
    <a href="${createTagFile(tag)}"><button id="${tag.id}">${tag.title}</button></a>
  `;
}

function createPostServableFile(post) {
  const dir = path.resolve(path.join(__dirname, '..', 'posts'));

  if (!fs.existsSync(dir)) {
    fs.mkdirSync(dir);
  }

  const baseFilePath = path.join(__dirname, '..', post.url);
  const stats = fs.statSync(baseFilePath);
  const content = fs.readFileSync(baseFilePath);
  const fileName = post.url.substring(post.url.lastIndexOf('/') + 1);
  const filePath = path.join(dir, fileName);
  fs.writeFileSync(filePath,`
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
	  <meta charset="utf-8">
	  <link rel="stylesheet" href="/index.css">
    <link rel="stylesheet" href="/posts.css">
	  <title>Matthew Kosarek</title>
	  <link rel="shortcut icon" href="/favicon/favicon.ico" type="image/x-icon">
  </head>
  <body>
	  <header>
      <h1>Matthew Kosarek</h1>
      <nav>
        <ul>
          <li><a href='/'>&#127969; Home</a></li>
          <li><a href='/resume.html'>&#128216; CV</a></li>
          <li><a href="https://www.linkedin.com/in/matthew-kosarek/">🏢 LinkedIn</a></li>
          <li><a href='/posts.html'>&#128221; Posts</a></li>
        </ul>
      </nav>
	  </header>

    <article>
    <h2>${post.title}</h2>
    <h3>Created ${stats.birthtime.toLocaleString()}. Last updated: ${stats.mtime.toLocaleString()}</h3>
    ${content}
    </article>
  </body>
</html>
  `)
  return '/posts/' + fileName;
}

function createPostLink(post) {
  return `
    <li><a href="${createPostServableFile(post)}">${post.title}</a></li>
  `;
}

function createPostPage(outputFile, postFilter) {
  const output = `
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
	  <meta charset="utf-8">
	  <link rel="stylesheet" href="/index.css">
    <link rel="stylesheet" href="/posts.css">
	  <title>Matthew Kosarek</title>
	  <link rel="shortcut icon" href="/favicon/favicon.ico" type="image/x-icon">
  </head>
  <body>
	  <header>
      <h1>Matthew Kosarek</h1>
      <nav>
        <ul>
          <li><a href='/'>&#127969; Home</a></li>
          <li><a href='/resume.html'>&#128216; CV</a></li>
          <li><a href="https://www.linkedin.com/in/matthew-kosarek/">🏢 LinkedIn</a></li>
          <li><a href='/posts.html'>&#128221; Posts</a></li>
        </ul>
      </nav>
	  </header>

    ${postFilter ? '' : `<section>
    <h2>Tags</h2>

    <div id='tag_list'>
        ${tags.map(createTag)}
    </div>
  </section`}

    <section>
    <h2>Posts</h2>
    <ul id='post_list'>
        ${posts.filter(post => !postFilter || post.tags.includes(postFilter)).map(createPostLink)}
    </ul>
    </section>
  </body>
</html>
`

  fs.writeFileSync(outputFile, output);
}

createPostPage(path.join(__dirname, '..', 'posts.html'));