Browse Source

function getMDTitles done

master
filesite 3 years ago
parent
commit
b4ebfb4719
  1. 21
      lib/DirScanner.php
  2. 15
      test/DirScannerTest.php
  3. 17
      test/init_test_files.sh

21
lib/DirScanner.php

@ -516,4 +516,25 @@ Class DirScanner { @@ -516,4 +516,25 @@ Class DirScanner {
return $menus;
}
//获取.md文件中的h1-h6标题
public function getMDTitles($id) {
$file = $this->scanResults[$id];
if (empty($file)) {return [];}
$titles = [];
$content = file_get_contents($file['realpath']);
preg_match_all('/#(.+)/u', $content, $matches);
if (!empty($matches[1])) {
foreach($matches[1] as $title) {
$num = substr_count($title, '#');
$titles[] = [
'name' => trim(str_replace('#', '', $title)),
'heading' => 'h' . ($num+1),
];
}
}
return $titles;
}
}

15
test/DirScannerTest.php

@ -70,16 +70,23 @@ $dirTree = $scanner->scan(__DIR__ . '/content/', 4); @@ -70,16 +70,23 @@ $dirTree = $scanner->scan(__DIR__ . '/content/', 4);
echo "Time cost: {$scanner->scanTimeCost} ms\n";
echo "\n";
$menus = $scanner->getMenus();
print_r($menus);
echo "\n";
echo "\n";
$readme_id = '34528ca632426cd2cd570dd32c9dcf4c';
$readme_titles = $scanner->getMDTitles($readme_id);
print_r($readme_titles);
//$menus = $scanner->getMenus();
//echo "Directories:\n";
//print_r($menus);
//echo "\n";
//echo "\n";
//echo "Directories and files' tree:\n";
//print_r($dirTree);
//echo "\n";
//echo "\n";
//$scanResults = $scanner->getScanResults();
//print_r($scanResults);
//echo "\n";

17
test/init_test_files.sh

@ -23,9 +23,26 @@ rm -rf content/ @@ -23,9 +23,26 @@ rm -rf content/
mkdir -p content/favs/
cd content/favs/
rm -f Readme.md
rm -rf "小说/"
rm -rf "图片/"
touch "Readme.md"
tee -a "Readme.md" <<EOF
# Favs我的收藏
把我收藏的常用网址分享给大家。
## 图片
常用的图片网站。
## 小说
常用的小说网站。
EOF
mkdir -p "小说/金庸小说/"
mkdir -p "小说/古龙小说/"
mkdir -p "图片/图片搜索/"

Loading…
Cancel
Save