耀 の 部落阁
页面内容 - 旧版日记仿 markdown 编译代码
旧版日记仿 markdown 编译代码
2019-2-21
2019-2-21
359
3 分钟

旧版日记仿 markdown 编译代码

const fs = require('hexo-fs');

const myDiary = fs.readFileSync('source/my.diary');

var timeRangeAndDiaries = myDiary.split(/-{9,}/); // Split by /-{9,}/ so that having time ranges and diaries separated (trimmed).

for (var i = 0; i < timeRangeAndDiaries.length; i++) {
    timeRangeAndDiaries[i] = timeRangeAndDiaries[i].trim();

    if (timeRangeAndDiaries[i].startsWith('#')) { // Edit time ranges to have h2 tags covered.
        timeRangeAndDiaries[i] = '<h2>' + timeRangeAndDiaries[i].replace('#', '').trim() + '</h2>';
    } else {
        // switch \n to {newline}
        timeRangeAndDiaries[i] = timeRangeAndDiaries[i].replace(/\n+/g, '{newline}');

        // Search list tags in each diaries and save them into temps for parts of paragraph.
        var tempLists = timeRangeAndDiaries[i].match(/\[list\](.*?)\[\/list\]/g);

        // Search img tags in each diaries and save them into temps for parts of paragraph.
        var tempImgs = timeRangeAndDiaries[i].match(/\[img\](.*?)\[\/img\]/g);

        // Edit the saved temps for parts of paragraph to html tags and replace them to each diary (clear out newline characters).
        if (tempLists != null) {
            for (var j = 0; j < tempLists.length; j++) {
                var temp = tempLists[j].replace(/\[\/?list\]/g, '').split('{newline}');
                for (var k = 0; k < temp.length; k++) {
                    var trimmed = temp[k].replace(/({newline})+/g, '').trim();
                    if (trimmed == '') {
                        temp[k] = '';
                    } else {
                        temp[k] = '<li>' + trimmed + '</li>';
                    }
                }
                tempLists[j] = '<ul>' + temp.join('') + '</ul>';
                timeRangeAndDiaries[i] = timeRangeAndDiaries[i].replace(/\[list\](.*?)\[\/list\]/, tempLists[j]);
            }
        }
        if (tempImgs != null) {
            for (var j = 0; j < tempImgs.length; j++) {
                tempImgs[j] = '<img src="' + tempImgs[j].replace(/\[\/?img\]/g, '').replace(/({newline})+/g, '').trim() + '">';
                timeRangeAndDiaries[i] = timeRangeAndDiaries[i].replace(/\[img\](.*?)\[\/img\]/, tempImgs[j]);
            }
        }

        // Split each diary by newline characters and save them into temps for paragraphing.
        var tempParagraphing = timeRangeAndDiaries[i].replace(/\n/g, '').split('{newline}');

        // Edit the saved temps for paragraphing to html tags and replace them to each diary (clear out newline characters and empty lines).
        for (var j = 0; j < tempParagraphing.length; j++) {
            var trimmed = tempParagraphing[j].replace(/({newline})+/g, '').trim();
            if (trimmed == '') {
                tempParagraphing[j] = '';
            } else if (!trimmed.startsWith('<ul')) {
                tempParagraphing[j] = '<p>' + trimmed + '</p>';
            }
        }
        var tempDiary = tempParagraphing.join('');
        if (tempDiary.trim() != '') {
            timeRangeAndDiaries[i] = '<div class="diary-item">' + tempDiary + '</div>';
        }
    }
}

// Join these converted time ranges and diaries into string.
var html = timeRangeAndDiaries.join('');
// console.log(html)
fs.writeFileSync('public/diaries.html', html);
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
以上内容结束 感谢您的阅读
留言