Skip to content

Commit

Permalink
feat: 命名约定兼容文件名中间有'.'
Browse files Browse the repository at this point in the history
  • Loading branch information
xugaoyi committed Apr 8, 2022
1 parent 3844f83 commit b6ecfe0
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"vuepress-plugin-one-click-copy": "^1.0.2",
"vuepress-plugin-thirdparty-search": "^1.0.2",
"vuepress-plugin-zooming": "^1.1.7",
"vuepress-theme-vdoing": "^1.10.2",
"vuepress-theme-vdoing": "^1.10.3",
"yamljs": "^0.3.0"
}
}
10 changes: 8 additions & 2 deletions vdoing/components/ArticleInfo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,18 @@ export default {
// 分类采用解析文件夹地址名称的方式 (即使关闭分类功能也可以正确跳转目录页)
const relativePathArr = relativePath.split('/')
const classifyArr = relativePathArr[0].split('.')
// const classifyArr = relativePathArr[0].split('.')
relativePathArr.forEach((item, index) => {
const nameArr = item.split('.')
if (index !== relativePathArr.length - 1) {
this.classifyList.push(nameArr[1] || nameArr[0] || '')
if (nameArr === 1) {
this.classifyList.push(nameArr[0])
} else {
const firstDotIndex = item.indexOf('.');
this.classifyList.push(item.substring(firstDotIndex + 1) || '')
}
}
})
Expand Down
23 changes: 21 additions & 2 deletions vdoing/node_utils/getSidebarData.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,26 @@ function mapTocToSidebar(root, collapsable, prefix = '') {
if (filename === '.DS_Store') { // 过滤.DS_Store文件
return
}
let [order, title, type] = filename.split('.');
// let [order, title, type] = filename.split('.');

const fileNameArr = filename.split('.')
const isDir = stat.isDirectory()
let order = '', title = '', type = '';
if (fileNameArr.length === 2) {
order = fileNameArr[0];
title = fileNameArr[1];
} else {
const firstDotIndex = filename.indexOf('.');
const lastDotIndex = filename.lastIndexOf('.');
order = filename.substring(0, firstDotIndex);
type = filename.substring(lastDotIndex + 1);
if (isDir) {
title = filename.substring(firstDotIndex + 1);
} else {
title = filename.substring(firstDotIndex + 1, lastDotIndex);
}
}

order = parseInt(order, 10);
if (isNaN(order) || order < 0) {
log(chalk.yellow(`warning: 该文件 "${file}" 序号出错,请填写正确的序号`))
Expand All @@ -127,7 +146,7 @@ function mapTocToSidebar(root, collapsable, prefix = '') {
if (sidebar[order]) { // 判断序号是否已经存在
log(chalk.yellow(`warning: 该文件 "${file}" 的序号在同一级别中重复出现,将会被覆盖`))
}
if (stat.isDirectory()) { // 是文件夹目录
if (isDir) { // 是文件夹目录
sidebar[order] = {
title,
collapsable, // 是否可折叠,默认true
Expand Down
18 changes: 10 additions & 8 deletions vdoing/node_utils/modules/readFileList.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const path = require('path'); // 路径模块
const chalk = require('chalk') // 命令行打印美化
const log = console.log

function readFileList (dir, filesList = []) {
function readFileList(dir, filesList = []) {
const files = fs.readdirSync(dir);
files.forEach((item, index) => {
let filePath = path.join(dir, item);
Expand All @@ -16,18 +16,20 @@ function readFileList (dir, filesList = []) {
} else {
if (path.basename(dir) !== 'docs') { // 过滤docs目录级下的文件

const fileNameArr = path.basename(filePath).split('.')
const filename = path.basename(filePath)
const fileNameArr = filename.split('.')
const firstDotIndex = filename.indexOf('.');
const lastDotIndex = filename.lastIndexOf('.');

let name = null, type = null;
if (fileNameArr.length === 2) { // 没有序号的文件
name = fileNameArr[0]
type = fileNameArr[1]
} else if (fileNameArr.length === 3) { // 有序号的文件
name = fileNameArr[1]
type = fileNameArr[2]
} else { // 超过两个‘.’的
log(chalk.yellow(`warning: 该文件 "${filePath}" 没有按照约定命名,将忽略生成相应数据。`))
return
} else if (fileNameArr.length >= 3) { // 有序号的文件(或文件名中间有'.')
name = filename.substring(firstDotIndex + 1, lastDotIndex)
type = filename.substring(lastDotIndex + 1)
}

if (type === 'md') { // 过滤非md文件
filesList.push({
name,
Expand Down
5 changes: 4 additions & 1 deletion vdoing/node_utils/setFrontmatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ function getCategories(file, categoryText) {
let ind = filePathArr.indexOf('docs')
if (ind !== -1) {
while (filePathArr[++ind] !== undefined) {
categories.push(filePathArr[ind].split('.').pop()) // 获取分类
const item = filePathArr[ind]
const firstDotIndex = item.indexOf('.');
categories.push(item.substring(firstDotIndex + 1) || '') // 获取分类
// categories.push(filePathArr[ind].split('.').pop()) // 获取分类
}
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion vdoing/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vuepress-theme-vdoing",
"version": "1.10.2",
"version": "1.10.3",
"description": "Vdoing theme for VuePress. 一个基于VuePress的知识管理兼博客主题。",
"author": {
"name": "gaoyi(Evan) Xu"
Expand Down
14 changes: 8 additions & 6 deletions vdoing/styles/index.styl
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,14 @@ h2

h3
font-size 1.35rem
h4
font-size 1.25rem
h5
font-size 1.15rem
h6
font-size 1.05rem

.page
h4
font-size 1.25rem
h5
font-size 1.15rem
h6
font-size 1.05rem

a.header-anchor
font-size 0.85em
Expand Down

1 comment on commit b6ecfe0

@vercel
Copy link

@vercel vercel bot commented on b6ecfe0 Apr 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.