Skip to content

Commit

Permalink
fix font scanning
Browse files Browse the repository at this point in the history
  • Loading branch information
guFalcon committed May 4, 2024
1 parent 1839869 commit 6e0f38a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
6 changes: 4 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const DOMPurify = createDOMPurify(window);
const app = express();
app.set("trust proxy", true);

import { scanFiles, preParse, manipulateHtml, wrapInPage, wrapInReveal, splitForReveal, parseFirstLineForPermissions } from "./obsidian.js";
import { scanFiles, scanFonts, preParse, manipulateHtml, wrapInPage, wrapInReveal, splitForReveal, parseFirstLineForPermissions } from "./obsidian.js";
import { hasSomeRoles } from "./utils.js";

async function sanitizeAndParseMarkdown(data, req) {
Expand Down Expand Up @@ -300,7 +300,9 @@ initKeycloak(app).then(() => {

const basePath = process.env.NEXT_PUBLIC_IS_APP_FOLDER ? '/app/' : '.';
scanFiles("md/", path.join(basePath, "md")).then(() => {
app.listen(process.env.NEXT_PUBLIC_PORT, "0.0.0.0");
scanFonts(path.join(basePath, "assets")).then(() => {
app.listen(process.env.NEXT_PUBLIC_PORT, "0.0.0.0");
});
});

// If file is not found, redirect to the start page.
Expand Down
46 changes: 46 additions & 0 deletions obsidian.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,52 @@ function makeSafeForCSS(name) {
});
}

export async function scanFonts(dir, root = dir) {
const files = fs.readdirSync(dir);

for (const file of files) {
try {
if (dir && file) {
// Check if dir and file are not null
const filePath = path.join(dir, file);
const isDirectory = fs.statSync(filePath).isDirectory();
if (isDirectory) {
const nmods = process.env.NEXT_PUBLIC_IS_APP_FOLDER
? "/app/node_modules"
: "node_modules";
const slides = process.env.NEXT_PUBLIC_IS_APP_FOLDER
? "/app/slides"
: "slides";
if (
filePath.startsWith(".") ||
filePath.startsWith(nmods) ||
filePath.startsWith(slides)
)
continue;
scanFonts(filePath, root);
} else {
// All other files.
const fileName = path.basename(file);
const relativePath = path.relative(root, filePath);
const p = relativePath.replace(/\\/g, "/");
if (p.startsWith("main-fonts/")) {
const fontName = path.basename(file, ".ttf");
mainFonts[fontName] = "assets/" + p;
mainFontsArray.push(fontName);
}
if (p.startsWith("nav-fonts/")) {
const fontName = path.basename(file, ".ttf");
navFonts[fontName] = "assets/" + p;
navFontsArray.push(fontName);
}
}
}
} catch (err) {
console.error(`Error reading file while scanning fonts ${file}`, err);
}
}
}

/**
* If it's the root dir, dirPrefix should be an empty string.
*/
Expand Down

0 comments on commit 6e0f38a

Please sign in to comment.