文章

md转换成_post下直接使用的文件

md转换成_post下直接使用的文件

md转换成_post下直接使用的文件

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
package org.example;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

public class Main {
    enum FileTime {
        CREATE, MODIFY, ACCESS;
    }

    /*
            ---
            title: 排序
            date: 2023-09-03 13:14:22 +0800
            categories: [algorithm, summary]
            tags: [自定义标签1, 自定义标签2]
            media_subpath: /assets/media/pictures/
            description: haha
            ---
    */
    public static final String FILE_PATH = "C:\\Users\\Spring\\Downloads\\algorithm";
    public static final String DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";
    // md 文档中所有图片路径的父路径
    public static final String MEDIA_SUB_PATH = "/assets/media/pictures/";

    public static void main(String[] args) throws Exception {
        File file = new File(FILE_PATH);
        traverseDirectory(file);
    }

    public static void main2(String[] args) throws IOException {
        String s = "C:\\Users\\Spring\\Downloads\\algorithm\\summary\\链表反转.md";
        File file = new File(s);
        if (isMdFile(file.getName()))
            System.out.println(getFileTime(new File(s), FileTime.MODIFY));
    }

    // 将 markdown 文件转换成可以直接放在 _posts 文件夹下发布的文件
    public static void convert(File file) throws Exception {
        String dateStr = getFileTime(file, FileTime.MODIFY);
        String originFileName = file.getName();
        // 插入前页
        insertFrontMatter(file, dateStr, originFileName);
        // 加前缀
        addDatePrefix(file, dateStr);
    }

    public static void insertFrontMatter(File file, String dateStr, String originFileName) throws Exception {
        String inserted = "---\n" +
                "title: " + getName(originFileName) + "\n" +
                "date: " + dateStr + " +0800\n" +
                "categories: " + "[" +
                "]\n" +
                "tags: " + "[" +
                "]\n" +
                "media_subpath: " + MEDIA_SUB_PATH + "\n" +
                "description: " +
                "\n" +
                "---\n";
        insertFileHeader(inserted.getBytes(StandardCharsets.UTF_8), file);
    }

    // 给文件增加日期前缀
    public static void addDatePrefix(File file, String dateStr) {
        String newName = file.getParent() + "\\" + dateStr.split(" ")[0] + "-" + file.getName();
        File newFile = new File(newName);
        if (!file.renameTo(newFile))
            System.out.println("rename error");
    }


    private static void insertFileHeader(byte[] header, File file) throws Exception {
        RandomAccessFile src = new RandomAccessFile(file, "rw");
        int srcLength = (int) src.length();
        byte[] buff = new byte[srcLength];
        src.read(buff, 0, srcLength);
        src.seek(0);
        src.write(header);
        src.seek(header.length);
        src.write(buff);
        src.close();
    }


    private static String getFileTime(File file, FileTime fileTime) throws IOException {
        if (file == null) return null;
        Path path = file.toPath();
        BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class);
        if (attr == null) return null;

        Instant instant = null;
        switch (fileTime) {
            // 创建时间
            case CREATE:
                instant = attr.creationTime().toInstant();
                break;
            // 更新时间
            case MODIFY:
                instant = attr.lastModifiedTime().toInstant();
                break;
            // 上次访问时间
            case ACCESS:
                attr.lastAccessTime().toInstant();
                break;
            default:
        }
        if (instant == null) return null;
        return DateTimeFormatter.ofPattern(DATE_PATTERN).withZone(ZoneId.systemDefault()).format(instant);
    }

    private static void traverseDirectory(File directory) throws Exception {
        // 列出目录下的所有文件和子目录
        File[] filesAndDirs = directory.listFiles();
        if (filesAndDirs == null) return;
        // 遍历每个文件和子目录
        for (File fileOrDir : filesAndDirs) {
            if (fileOrDir.isFile()) {
                // 是 markdown 文件才处理
                if (isMdFile(fileOrDir.getName())) {
                    convert(fileOrDir);
                }
            } else if (fileOrDir.isDirectory()) {
                // 如果是目录,递归遍历子目录
                traverseDirectory(fileOrDir);
            }
        }
    }

    // 去除扩展名
    private static String getName(String fileName) {
        return fileName.substring(0, fileName.lastIndexOf('.'));
    }

    // 判断是否是 markdown 文件
    private static boolean isMdFile(String fileName) {
        String extension = fileName.substring(fileName.lastIndexOf('.'));
        return ".md".equals(extension) || ".markdown".equals(extension);
    }

}
本文由作者按照 CC BY 4.0 进行授权