@wxn0brp/falcon-frame 0.6.1 → 0.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -44,6 +44,6 @@ export declare class FalconFrame<Vars extends Record<string, any> = Record<strin
44
44
  }
45
45
  export default FalconFrame;
46
46
  export * as Helpers from "./helpers.js";
47
- export { FFOpts as Opts } from "./types.js";
47
+ export type { FFOpts as Opts } from "./types.js";
48
48
  export { validateBody } from "./valid.js";
49
49
  export { FFRequest, FFResponse, renderHTML, RouteHandler, Router };
package/dist/render.d.ts CHANGED
@@ -1,2 +1,5 @@
1
1
  import FalconFrame from "./index.js";
2
- export declare function renderHTML(templatePath: string, data?: Record<string, any>, renderedPaths?: string[], FF?: FalconFrame): string;
2
+ export interface RenderOptions {
3
+ noLayout?: boolean;
4
+ }
5
+ export declare function renderHTML(templatePath: string, data?: Record<string, any>, renderedPaths?: string[], FF?: FalconFrame, opts?: RenderOptions): string;
package/dist/render.js CHANGED
@@ -1,27 +1,35 @@
1
1
  import fs from "fs";
2
2
  import path from "path";
3
- export function renderHTML(templatePath, data = {}, renderedPaths = [], FF) {
3
+ export function renderHTML(templatePath, data = {}, renderedPaths = [], FF, opts = {}) {
4
4
  try {
5
5
  const realPath = path.resolve(templatePath);
6
6
  if (renderedPaths.includes(realPath))
7
7
  return `<!-- Circular dependency detected: tried to render ${templatePath} again -->`;
8
- const FFData = FF && FF.getVar("render data");
9
- if (FFData) {
10
- data = {
11
- ...FFData,
12
- ...data,
13
- };
14
- }
15
8
  let template = fs.readFileSync(templatePath, "utf8");
9
+ // Loading internal data, e.g. <!-- data { "title": "My title" } -->
10
+ const templateDataMatch = template.match(/<!--\s*data\s*(\{.*?\})\s*-->/s);
11
+ let templateData = {};
12
+ if (templateDataMatch) {
13
+ try {
14
+ templateData = JSON.parse(templateDataMatch[1]);
15
+ template = template.replace(templateDataMatch[0], "");
16
+ }
17
+ catch (err) {
18
+ template = template.replace(templateDataMatch[0], "<!-- Invalid template data -->");
19
+ }
20
+ }
21
+ const FFData = FF && FF.getVar("render data");
22
+ data = {
23
+ ...(FFData || {}),
24
+ ...templateData,
25
+ ...data,
26
+ };
16
27
  // Inserting data, e.g. {{name}}
17
28
  template = template.replace(/{{(.*?)}}/g, (_, key) => data[key.trim()] || "");
18
29
  // Loading partials, e.g. <!-- include header -->
19
30
  template = template.replace(/<!--\s*include\s*(.*?)\s*-->/g, (_, partialName) => {
20
31
  const partialPath = path.join(path.dirname(templatePath), partialName + ".html");
21
- return renderHTML(partialPath, data, [
22
- ...renderedPaths,
23
- realPath,
24
- ], FF);
32
+ return renderHTML(partialPath, data, [...renderedPaths, realPath], FF, { noLayout: true });
25
33
  });
26
34
  // Loading files, e.g. /* include style.css */
27
35
  template = template.replace(/\/\*\s*include\s*(.*?)\s*\*\//g, (_, fileName) => {
@@ -35,15 +43,16 @@ export function renderHTML(templatePath, data = {}, renderedPaths = [], FF) {
35
43
  });
36
44
  // Layout
37
45
  const FFLayout = FF && FF.getVar("layout");
38
- if (FFLayout) {
46
+ if (!opts.noLayout && FFLayout) {
39
47
  const hasHtmlStructure = /<\s*html|<\s*body/i.test(template);
40
48
  const forceLayout = /<!--\s*force-layout\s*-->/.test(template);
41
49
  const forceNoLayout = /<!--\s*force-no-layout\s*-->/.test(template);
50
+ template = template.replace(/<!--\s*layout\s*-->|<!--\s*force-layout\s*-->|<!--\s*force-no-layout\s*-->/g, "");
42
51
  if (hasHtmlStructure && !forceLayout)
43
52
  return template;
44
53
  if (!hasHtmlStructure && forceNoLayout)
45
54
  return template;
46
- return renderHTML(FFLayout, { ...data, body: template }, [...renderedPaths, realPath], FF);
55
+ return renderHTML(FFLayout, { ...data, body: template }, [...renderedPaths, realPath], FF, { noLayout: true });
47
56
  }
48
57
  return template;
49
58
  }
package/package.json CHANGED
@@ -1,16 +1,28 @@
1
1
  {
2
2
  "name": "@wxn0brp/falcon-frame",
3
- "version": "0.6.1",
3
+ "version": "0.6.2",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "author": "wxn0brP",
7
7
  "license": "MIT",
8
8
  "type": "module",
9
+ "description": "Lightweight modular TypeScript web framework",
9
10
  "homepage": "https://github.com/wxn0brP/FalconFrame",
10
11
  "repository": {
11
12
  "type": "git",
12
13
  "url": "https://github.com/wxn0brP/FalconFrame.git"
13
14
  },
15
+ "keywords": [
16
+ "http",
17
+ "web",
18
+ "framework",
19
+ "typescript",
20
+ "modular",
21
+ "lightweight",
22
+ "middleware",
23
+ "middleware-first",
24
+ "middleware-chain"
25
+ ],
14
26
  "devDependencies": {
15
27
  "@types/node": "*",
16
28
  "tsc-alias": "*",