-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenIndex.php
99 lines (75 loc) · 2.87 KB
/
genIndex.php
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
<?php
/**
* genIndex.php用于生成文件夹下的索引文件,README.md (or. index.md)
* 需要PHP cli
* 运行cmd: php getIndex.php
*
* 也可以整合到deploy.sh
*
*/
// 定义文件夹,必须在docs路径下
$mainFolder = "demo";
$subFolder = "inner-demo";
$description = "DEMO"; // 用于描述标题
genIndex($mainFolder, $subFolder, $description);
function genIndex($mainFolder, $folder, $description)
{
$path = "docs/" . $mainFolder . "/" . $folder . "/";
$mainIndex = "docs/" . $mainFolder . "/README.md";
$match = $path . "*.md";
$articles = [];
foreach (glob($match) as $filename) {
if (!preg_match("/README\.md/", $filename)) {
$name = basename($filename);
$mTime = filemtime($filename);
$articles[] = [
"path" => $filename,
"url" => "/" . $mainFolder . "/" . $folder . "/" . $name,
"mTime" => $mTime,
];
}
}
usort($articles, function ($i1, $i2) {
return $i2['mTime'] <=> $i1['mTime'];
});
foreach ($articles as $i => $article) {
$content = file_get_contents($article['path']);
$title = "";
if (preg_match("/#([^#|\n]+)\n/smi", $content, $m)) {
$title = trim($m[1]);
}
$articles[$i]['title'] = $title;
}
$content = genMarkDown($articles, $description, $mainFolder);
file_put_contents($path . "/README.md", $content);
$latest = "\n";
foreach ($articles as $i => $article) {
if ($i === 5) {
break;
}
$latest .= "- [" . $article['title'] . "](" . $article['url'] . ") <span>- " . date('m/d/Y', $article['mTime']) . "</span>\n";
}
$latest .= "\n";
$mainIndexContent = file_get_contents($mainIndex);
// print($mainIndexContent);
$updatedMainIndexContent = preg_replace("/(<!--" . $folder . "START-->).*?(<!--" . $folder . "END-->)/smi", '${1}' . $latest . '${2}', $mainIndexContent);
file_put_contents($mainIndex, $updatedMainIndexContent);
}
function genMarkDown($articles, $description, $mainFolder)
{
$content = "---\n";
// $content .= "sidebar: false\n";
$content .= "title: " . $description . " - 文章列表\n";
$content .= "description: " . $description . " - 文章列表\n";
$content .= "---\n\n";
$content .= "# " . $description . " - 文章列表\n";
$content .= "\n";
// $content .= '<p><a href="/' . $mainFolder . '">Back ↩</a></p>';
// $content .= "\n\n";
foreach ($articles as $article) {
$content .= "- [" . $article['title'] . "](" . $article['url'] . ")\n";
// 如何需要显示文件最后编辑时间,可以参考下面的代码
// $content .= "- [" . $article['title'] . "](" . $article['url'] . ") <span>- Last Updated: " . date('m/d/Y H:i:s', $article['mTime']) . "</span>\n";
}
return $content;
}