Skip to content

Commit

Permalink
feat(#82): refactor dirParent func into utils/path.ts as getParentName
Browse files Browse the repository at this point in the history
  • Loading branch information
Tweekism committed Jul 14, 2024
1 parent 7cfbdae commit 4d876ea
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
14 changes: 3 additions & 11 deletions src/routes/viewer.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { lstatSync, readFileSync } from 'fs';
import { dirname as pdirname, basename as pbasename, join as pjoin } from 'path';
import { dirname as pdirname, join as pjoin } from 'path';

import { Request, Response, Router } from 'express';

import { messageClientsAt } from '../app';
import config from '../parser/config';
import { pathToURL, pcomponents, pmime } from '../utils/path';
import { pathToURL, pcomponents, pmime, getParentName } from '../utils/path';
import { renderDirectory, renderTextFile } from '../parser/parser';

export const router = Router();
Expand All @@ -22,14 +22,6 @@ const pageTitle = (path: string) => {
} else return pjoin(...comps.slice(-2));
};

const dirParent = (path: string) => {
let parent = pbasename(pdirname(path));
if (!parent) {
parent = '/ (root)';
}
return parent;
};

router.get(/.*/, async (req: Request, res: Response) => {
const path = res.locals.filepath;

Expand Down Expand Up @@ -75,7 +67,7 @@ router.get(/.*/, async (req: Request, res: Response) => {
${config.styles}
</style>
<body>
<a id="parent-dir" data-parent-folder="${dirParent(path)}" href="${pathToURL(pdirname(path))}"></a>
<a id="parent-dir" data-parent-folder="${getParentName(path)}" href="${pathToURL(pdirname(path))}"></a>
<div id="body-content">
${body}
</div>
Expand Down
8 changes: 8 additions & 0 deletions src/utils/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,11 @@ export const urlToPath = (url: string) => {

export const pathToURL = (path: string, route: string = 'viewer') =>
`/${route}${encodeURIComponent(path).replaceAll('%2F', '/')}`;

export const getParentName = (path: string) => {
let parentName = pbasename(pdirname(path));
if (!parentName) {
parentName = '/ (root)';
}
return parentName;
};

0 comments on commit 4d876ea

Please sign in to comment.