Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
sharpchen committed Jun 27, 2024
1 parent ebfa705 commit f57efad
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 45 deletions.
4 changes: 2 additions & 2 deletions docs/.vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const vitepressConfig = defineConfig({
markdown: {
lineNumbers: true,
theme: {
light: await themeService.getTheme('Eva Light'),
dark: await themeService.getTheme('Eva Dark'),
light: await themeService.getTheme('Eva-Light'),
dark: await themeService.getTheme('Eva-Dark'),
},
codeTransformers: [transformerTwoslash()],
},
Expand Down
2 changes: 1 addition & 1 deletion docs/services/EmojiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class FluentEmojiHandler extends EmojiHandler {
}
async getEmojiUrl(variant: EmojiVariant, emoji: DocumentIcon): Promise<string> {
const hex = this.getHexOfEmoji(emoji);
const match = (await githubService.fromRepository('bignutty/fluent-emoji').getTree()).filter(
const match = (await githubService.fromRepository('bignutty/fluent-emoji').getTree({})).filter(
x => x.path?.includes(hex) && x.path.includes('animated-static')
);
if (!match.length) throw new Error(`APNG path of emoji ${emoji} not found. Hex: ${hex}`);
Expand Down
59 changes: 42 additions & 17 deletions docs/services/GithubService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,48 @@ class GithubRepositoryEndPointMethods {
})
).data as RepoFileResponse;
}
async getTree(branchSHA?: string): Promise<RepoTreeResponse> {
return (
await octokit.rest.git.getTree({
owner: this.owner,
repo: this.repo,
tree_sha:
branchSHA ??
(
await octokit.rest.git.getRef({
owner: this.owner,
repo: this.repo,
ref: `heads/main`,
})
).data.object.sha,
recursive: 'true',
})
).data.tree;
async getTree(options: { branchSHA?: string; branch?: string }): Promise<RepoTreeResponse> {
let branch: string = options.branch ?? 'main';
let sha: string;
try {
sha =
options.branchSHA ??
(
await octokit.rest.git.getRef({
owner: this.owner,
repo: this.repo,
ref: `heads/${branch}}`,
})
).data.object.sha;
} catch (error) {
console.log(
`Error fetching ref of ${JSON.stringify({
repo: `${this.owner}/${this.repo}`,
branch: branch,
})}`,
error
);
throw error;
}
try {
return (
await octokit.rest.git.getTree({
owner: this.owner,
repo: this.repo,
tree_sha: sha,
recursive: 'true',
})
).data.tree;
} catch (error) {
console.log(
`Error fetching tree of ${JSON.stringify({
repo: `${this.owner}/${this.repo}`,
branch: branch,
})}`,
error
);
throw error;
}
}
async getFiles(dir: string, searchOption: 'top' | 'deep'): Promise<RepoFileResponse> {
const current = await this.fetchStructureByPath(dir);
Expand Down
9 changes: 3 additions & 6 deletions docs/services/IThemeService.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import * as shiki from 'shiki';
import { themes } from '../../.github/workflows/beforeBuild/sync-themes.mjs';
import { TextmateTheme } from '../../.github/workflows/beforeBuild/types.mjs';
export type ThemeName = keyof typeof themes;
import { RemoteThemeInfo, TextmateTheme, ThemeName } from './ThemeService';
// export type ThemeName = keyof typeof themes;
export interface IThemeService {
readonly innerThemeService: Awaited<ReturnType<typeof shiki.getSingletonHighlighter>>;
register(theme: TextmateTheme): Promise<void>;
getTheme(name: ThemeName): Promise<shiki.ThemeRegistration>;
themes(): any[];
isThemeRegistered(name: ThemeName): boolean;
parseTheme(path: string): TextmateTheme;
physicalPathOfTheme(name: ThemeName): string;
fetchThemeObject(themeInfo: RemoteThemeInfo): Promise<TextmateTheme>;
initializeRegistration(): Promise<void>;
}
59 changes: 40 additions & 19 deletions docs/services/ThemeService.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
import * as fs from 'fs';
import path from 'path';
import axios from 'axios';
import * as shiki from 'shiki';
import { themes as themeInfo } from '../../.github/workflows/beforeBuild/sync-themes.mjs';
import type { TextmateTheme } from '../../.github/workflows/beforeBuild/types.mjs';
import { projectRoot } from '../shared/FileSystem';
import { IThemeService, ThemeName } from './IThemeService';
import { getRepoFileInfo, githubService } from './GithubService';
import { IThemeService } from './IThemeService';
const highlighter = await shiki.getSingletonHighlighter();

type TextmateRule = {
name?: string;
scope: string;
settings: { fontStyle?: string; foreground?: string };
};
export type TextmateTheme = {
name: string;
tokenColors: TextmateRule[];
};
export type RemoteThemeInfo = {
repo: string;
path: string;
branch: string;
};

const themeInfos = {
'Eva-Light': { repo: 'fisheva/Eva-Theme', path: 'themes/Eva-Light.json', branch: 'master' },
'Eva-Dark': { repo: 'fisheva/Eva-Theme', path: 'themes/Eva-Dark.json', branch: 'master' },
} satisfies Record<string, RemoteThemeInfo>;
export type ThemeName = keyof typeof themeInfos;
class ThemeService implements IThemeService {
readonly innerThemeService: Awaited<ReturnType<typeof shiki.getSingletonHighlighter>> =
highlighter;
Expand All @@ -17,25 +35,28 @@ class ThemeService implements IThemeService {
if (!this.isThemeRegistered(name)) throw new Error(`Theme \`${name}\` not registered.`);
return this.innerThemeService.getTheme(name);
}
themes(): any[] {
throw new Error('Method not implemented.');
}
isThemeRegistered(name: ThemeName): boolean {
return this.innerThemeService.getLoadedThemes().includes(name);
}
physicalPathOfTheme(name: ThemeName): string {
const ret = path.join(projectRoot().fullName, `public/${name}.json`);
if (!fs.existsSync(ret)) throw new Error(`${name}.json does not exist at /public`);
return ret;
}
parseTheme(filePath: string): TextmateTheme {
return JSON.parse(fs.readFileSync(filePath, 'utf-8'));
async fetchThemeObject(info: RemoteThemeInfo): Promise<TextmateTheme> {
console.error('hello???');
const matches = (
await githubService.fromRepository(info.repo).getTree({ branch: info.branch })
).filter(x => x.path === info.path);
if (!matches.length) throw new Error();
const url = (await getRepoFileInfo(info.repo, matches[0].path!)).download_url!;
try {
const response = await axios.get<string>(url, { responseType: 'text' });
return (await import('jsonc-parser')).parse(response.data) as TextmateTheme;
} catch (error) {
console.error('Error fetching JSON data:', error);
throw error;
}
}
async initializeRegistration(): Promise<void> {
await Promise.all(
(Object.entries(themeInfo) as [ThemeName, string][]).map(async x => {
const p = this.physicalPathOfTheme(x[0]);
const json = this.parseTheme(p);
(Object.entries(themeInfos) as [ThemeName, RemoteThemeInfo][]).map(async x => {
const json = await this.fetchThemeObject(x[1]);
await this.register(json);
})
);
Expand Down

0 comments on commit f57efad

Please sign in to comment.