Skip to content

Commit

Permalink
grrr
Browse files Browse the repository at this point in the history
  • Loading branch information
sharpchen committed Sep 23, 2024
1 parent dd25095 commit 851fe5d
Show file tree
Hide file tree
Showing 6 changed files with 399 additions and 37 deletions.
8 changes: 4 additions & 4 deletions docs/services/FeatureService.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import matter from 'gray-matter';
import { type Feature } from 'vitepress/dist/client/theme-default/components/VPFeatures.vue';
import { DocumentName, documentService } from './DocumentService';
import type { Feature } from 'vitepress/dist/client/theme-default/components/VPFeatures.vue';
import { type DocumentName, documentService } from './DocumentService';
import { emojiService } from './EmojiService';
import { IFeatureService } from './IFeatureService';
import type { IFeatureService } from './IFeatureService';
class FeatureService implements IFeatureService {
readonly linkText: string = 'Get started';
getFeaturesAsYaml(): string {
Expand Down Expand Up @@ -35,7 +35,7 @@ class FeatureService implements IFeatureService {
link: documentService.tryGetIndexLinkOfDocument(key as DocumentName),
linkText: this.linkText,
};
})
}),
);
}
}
Expand Down
43 changes: 35 additions & 8 deletions docs/services/SidebarService.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { DefaultTheme } from 'vitepress';
import { DirectoryInfo, FileInfo, Path, documentRoot } from '../shared/FileSystem';
import { DocumentName, documentMap, documentService } from './DocumentService';
import { IDocumentService } from './IDocumentService';
import { ISidebarService } from './ISidebarService';
import { exec, spawnSync } from 'node:child_process';
import type { DefaultTheme } from 'vitepress';
import {
type DirectoryInfo,
type FileInfo,
Path,
documentRoot,
projectRoot,
} from '../shared/FileSystem';
import { type DocumentName, documentMap, documentService } from './DocumentService';
import type { IDocumentService } from './IDocumentService';
import type { ISidebarService } from './ISidebarService';
import { async } from 'fast-glob';
import path from 'node:path';
const solveSharpSign = (text: string) => {
if (text.includes('sharp')) return text.replace('sharp', '#');
if (text.includes('Sharp')) return text.replace('Sharp', '#');
Expand All @@ -12,7 +21,7 @@ class SidebarService implements ISidebarService {
private readonly base: string = `/${documentRoot().name}`;
readonly documentService: IDocumentService = documentService;
getMultipleSidebar(): DefaultTheme.SidebarMulti {
let sidebar: DefaultTheme.SidebarMulti = {};
const sidebar: DefaultTheme.SidebarMulti = {};
for (const name of Object.keys(documentMap)) {
sidebar[`${this.base}/${name}/docs/`] = this.getSidebarOfDocument(name as DocumentName);
}
Expand All @@ -23,14 +32,32 @@ class SidebarService implements ISidebarService {
return [
{
text: solveSharpSign(name),
items: this.transformFolderToSidebarItem(markdownEntry, `${this.base}/${name}`),
items:
name === 'Articles'
? this.transformFolderToSidebarItem(markdownEntry, `${this.base}/${name}`).sort(
compareTrackedDate,
)
: this.transformFolderToSidebarItem(markdownEntry, `${this.base}/${name}`),
},
];
function compareTrackedDate(a: DefaultTheme.SidebarItem, b: DefaultTheme.SidebarItem) {
return gitTrackedDate(a.link) - gitTrackedDate(b.link);
}
function gitTrackedDate(file: string): Date {
return new Date(
spawnSync('git', [
'log',
'-1',
'--pretty="%ai"',
path.join(documentRoot().fullName, file),
]).stdout.toString(),
);
}
}
transformFolderToSidebarItem(folder: DirectoryInfo, base: string): DefaultTheme.SidebarItem[] {
const subs = folder.getDirectories();
// load files in this folder
let items: DefaultTheme.SidebarItem[] = folder.getFiles().length
const items: DefaultTheme.SidebarItem[] = folder.getFiles().length
? filesToSidebarItems(folder.getFiles(), `${base}/${folder.name}`)
: [];
for (const index in subs) {
Expand Down
19 changes: 19 additions & 0 deletions docs/test/firstTrackedDate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { execSync, spawn, spawnSync } from 'node:child_process';
import { basename, dirname } from 'node:path';
import * as fs from 'node:fs';
import { expect, test } from 'vitest';
import path from 'node:path';
import { projectRoot } from '../shared/FileSystem';

// test('expect date', () => {
// const foo = spawnSync('git', [
// 'log',
// '-1',
// '--pretty="%ai"',
// path.join(projectRoot().fullName, 'package.json'),
// ]).stdout.toString();
// expect(foo.startsWith('2024')).toBe(true);
// });
test('', () => {
expect(projectRoot().parent?.fullName).toBe('/home/sharpchen/desktop/repo/sharpchen.github.io');
});
11 changes: 11 additions & 0 deletions docs/test/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { spawnSync } from 'node:child_process';
import { basename, dirname } from 'node:path';
import path from 'node:path';
import { projectRoot } from '../shared/FileSystem';

const foo = spawnSync('git', [
'log',
'-1',
'--pretty="%ai"',
path.join(projectRoot(), 'package.json'),
]).stdout.toString();
52 changes: 27 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
{
"scripts": {
"docs:dev": "vitepress dev docs",
"docs:build": "vitepress build docs",
"docs:preview": "vitepress preview docs"
},
"dependencies": {
"@types/markdown-it": "^13.0.8",
"fast-glob": "^3.3.2",
"gray-matter": "^4.0.3",
"linq": "^4.0.3",
"markdown-it": "^14.1.0",
"octokit": "^4.0.2",
"vue": "^3.4.29"
},
"devDependencies": {
"vitepress": "latest",
"@shikijs/vitepress-twoslash": "^1.9.0",
"@types/node": "^20.14.5",
"axios": "^1.7.2",
"jsonc-parser": "^3.2.1",
"mermaid": "^10.9.1",
"shiki": "^1.7.0",
"vitepress-plugin-mermaid": "^2.0.16"
},
"type": "module"
"scripts": {
"docs:dev": "vitepress dev docs",
"docs:build": "vitepress build docs",
"docs:preview": "vitepress preview docs",
"test": "vitest"
},
"dependencies": {
"@types/markdown-it": "^13.0.8",
"fast-glob": "^3.3.2",
"gray-matter": "^4.0.3",
"linq": "^4.0.3",
"markdown-it": "^14.1.0",
"octokit": "^4.0.2",
"vue": "^3.4.29"
},
"devDependencies": {
"@shikijs/vitepress-twoslash": "^1.9.0",
"@types/node": "^20.14.5",
"axios": "^1.7.2",
"jsonc-parser": "^3.2.1",
"mermaid": "^10.9.1",
"shiki": "^1.7.0",
"vitepress": "latest",
"vitepress-plugin-mermaid": "^2.0.16",
"vitest": "^2.1.1"
},
"type": "module"
}
Loading

0 comments on commit 851fe5d

Please sign in to comment.