Skip to content

Commit de64861

Browse files
committed
feat: allow custom views directory for templates and markdown
- Add viewsDir parameter to LiteNode constructor - Modify MarkdownHandler to accept custom views directory - Update extendResponse to use configurable views path - Pass viewsDir through handleRequest chain - Maintain backward compatibility with default "views" directory - Update TypeScript definitions to reflect new constructor params
1 parent 721b5e2 commit de64861

File tree

5 files changed

+38
-17
lines changed

5 files changed

+38
-17
lines changed

core/LiteNode/LiteNode.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,16 @@ export class LiteNode {
2929
#errorHandler
3030
#middlewareStack
3131
#handleRequest
32-
#directory
32+
#staticDir
33+
#viewsDir
3334
#staticAssetLoader
3435
#markdownHandler
3536

36-
constructor(directory = "static") {
37+
constructor(staticDir = "static", viewsDir = "views") {
38+
// If staticDir is provided but viewsDir isn't, use default views
39+
this.#staticDir = staticDir
40+
this.#viewsDir = viewsDir
41+
3742
// Initialize the root node for routing
3843
this.#rootNode = new RouteNode()
3944

@@ -55,7 +60,8 @@ export class LiteNode {
5560
this.#notFoundHandler,
5661
this.#errorHandler,
5762
nativeReq,
58-
nativeRes
63+
nativeRes,
64+
this.#viewsDir
5965
)
6066
} catch (error) {
6167
console.error(`Error handling request: ${error.message}`)
@@ -64,12 +70,13 @@ export class LiteNode {
6470
}
6571
}
6672

67-
this.#directory = directory
68-
if (directory !== "__NO_STATIC_DIR__") {
69-
this.#staticAssetLoader = new StaticAssetLoader(directory)
73+
this.#staticDir = staticDir
74+
if (staticDir !== "__NO_STATIC_DIR__") {
75+
this.#staticAssetLoader = new StaticAssetLoader(staticDir)
7076
}
7177

72-
this.#markdownHandler = new MarkdownHandler() // Initialize the MarkdownHandler
78+
// Initialize MarkdownHandler with the views directory
79+
this.#markdownHandler = new MarkdownHandler(this.#viewsDir)
7380
}
7481

7582
printTree() {
@@ -160,7 +167,7 @@ export class LiteNode {
160167
async renderToFile(template, data, outputPath) {
161168
try {
162169
// Render a template to a file
163-
const templateEngine = new STE("views")
170+
const templateEngine = new STE(this.#viewsDir)
164171
const html = await templateEngine.render(template, data)
165172
await writeFile(outputPath, html, "utf-8")
166173
} catch (error) {

core/LiteNode/methods/extendResponse.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { STE } from "../../STE/ste.js"
22
import { getContentType } from "../../utils/getContentType.js"
33
import { readFileSync } from "node:fs"
44

5-
export function extendResponse(nativeRes) {
5+
export function extendResponse(nativeRes, viewsDir) {
66
nativeRes.redirect = (location, statusCode = 302) => {
77
nativeRes.writeHead(statusCode, { Location: location })
88
nativeRes.end()
@@ -32,7 +32,7 @@ export function extendResponse(nativeRes) {
3232

3333
nativeRes.render = async (template, data) => {
3434
try {
35-
const templateEngine = new STE("views")
35+
const templateEngine = new STE(viewsDir)
3636
const html = await templateEngine.render(template, data)
3737
nativeRes.setHeader("Content-Type", "text/html")
3838
nativeRes.end(html)

core/LiteNode/methods/handleRequest.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@ import { extendResponse } from "./extendResponse.js"
22
import { applyMiddleware } from "./applyMiddleware.js"
33
import { findRouteHandler } from "./findRouteHandler.js"
44

5-
export async function handleRequest(middlewareStack, routeNode, notFoundHandler, errorHandler, nativeReq, nativeRes) {
5+
export async function handleRequest(
6+
middlewareStack,
7+
routeNode,
8+
notFoundHandler,
9+
errorHandler,
10+
nativeReq,
11+
nativeRes,
12+
viewsDir
13+
) {
614
try {
7-
extendResponse(nativeRes)
15+
extendResponse(nativeRes, viewsDir)
816

917
await applyMiddleware(middlewareStack, nativeReq, nativeRes)
1018

core/LiteNode/methods/markdownHandler.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ import { join, relative } from "node:path"
33
import { SMP } from "../../SMP/smp.js"
44

55
export class MarkdownHandler {
6-
constructor() {
7-
this.smp = new SMP() // Initialize the custom markdown parser
6+
#viewsDir
7+
8+
constructor(viewsDir = "views") {
9+
this.#viewsDir = viewsDir
10+
// Initialize the custom markdown parser and pass viewsDir to SMP
11+
this.smp = new SMP(viewsDir)
812
}
913

1014
/**
@@ -23,10 +27,12 @@ export class MarkdownHandler {
2327
*/
2428
async parseMarkdownFileS(dir) {
2529
const normalizedDir = dir.startsWith("/") ? dir.slice(1) : dir
26-
const files = await this.getMarkdownFiles(join("views", normalizedDir))
30+
// Use the custom views directory instead of hardcoded "views"
31+
const files = await this.getMarkdownFiles(join(this.#viewsDir, normalizedDir))
2732
return Promise.all(
2833
files.map((file) => {
29-
const relativePath = relative("views", file)
34+
// Use the custom views directory instead of hardcoded "views"
35+
const relativePath = relative(this.#viewsDir, file)
3036
return this.parseMarkdownFile(relativePath)
3137
})
3238
)

types/litenode.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ declare module "litenode" {
9696

9797
// LiteNode class with method signatures
9898
class LiteNode {
99-
constructor(directory?: string)
99+
constructor(staticDir?: string, viewsDir?: string)
100100

101101
/**
102102
* Prints the routing tree to the console.

0 commit comments

Comments
 (0)