Skip to content

Commit

Permalink
feat: add automated typography support (#22)
Browse files Browse the repository at this point in the history
* feat: add automated typography support

* fix: lint

* 1.9.0
  • Loading branch information
sapachev authored Jun 8, 2023
1 parent 35e22df commit 71b61c7
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 16 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Lightweight tool to create static custom HTTP Error Pages in minimalistic adapti
* Customization
* Localization (i18n)
* Accessibility (a11y)
* Automated typography support
* Built-in web server config generator (Nginx)


Expand Down Expand Up @@ -115,12 +116,14 @@ Every locale file can be extended with any number of variables that you want to

To localize your pages, just create a new directory in the `src` with any pages you want to generate. You are able to choose any set of HTTP status codes (for example, only for the 4xx errors), just follow the naming convention and don’t forget to extract common messages to the `common.json` file of your locale.

All texts are processing with [Typograf](https://github.com/typograf/typograf) library by default. This works automatically, so you don't need to adapt texts in the source directory. In case of new locale adding, please check a list of [supported locales](https://github.com/typograf/typograf/blob/dev/docs/LOCALES.en-US.md) and update locales mapping in `TYPOGRAF_LOCALES` constant.


### Server Configurations

During the build process, the Tool will automatically create a config snippet for your server. This snippet will contain information about handled errors and locations to reach the pages that represent them. At the moment, the only Nginx server supported.

You just need to copy all files from the `dist` directory to your server and apply automatically created config snippet to existing server configuration. According to the snippet template, all pages must be located in the `/usr/share/nginx/html/error-pages` directory. In case if you want to change something in a snippet, you can edit the template in the `snippets` directory. Same as for the page templates, these templates support the mustache.js engine, so you can use any render logic you want (lists, conditions, etc).
You just need to copy all files from the `dist` directory to your server and apply automatically created config snippet to existing server configuration. According to the snippet template, all pages must be located in the `/usr/share/nginx/html/error-pages` directory. In case if you want to change something in a snippet, you can edit the template in the `snippets` directory. Same as for the page templates, these templates support the mustache.js engine, so you can use any render logic you want (lists, conditions, loops, etc).

The config snippet itself I would recommend to place in the `/etc/nginx/snippets/` directory and use the following line to include it to your configuration: `include /etc/nginx/snippets/nginx-error-pages.conf;`.

Expand Down
4 changes: 2 additions & 2 deletions lib/classes/Compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ILogger } from "./Logger";
import { Messages } from "./Messages";
import { Renderer } from "./Renderer";

import { Config, TemplateVariables } from "../interfaces";
import { Config, SnippetVariables, TemplateVariables } from "../interfaces";
import { DI_TOKENS } from "../tokens";
import { MessagesEnum } from "../../messages";
import { PathRegistry } from "./PathRegistry";
Expand Down Expand Up @@ -102,7 +102,7 @@ export class Compiler implements ICompiler {

this.logger.print(Messages.list(path));

await this.fsHelper.writeFile(path, Renderer.renderTemplate(template, { codes: Array.from(list) }));
await this.fsHelper.writeFile(path, Renderer.renderSnippet(template, { codes: Array.from(list) } as SnippetVariables));
})
);
} else {
Expand Down
14 changes: 13 additions & 1 deletion lib/classes/Renderer.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import { render } from "mustache";
import Typograf from "typograf";

import { SnippetVariables, TemplateVariables } from "../interfaces";
import { TYPOGRAF_LOCALES } from "../constants";

export class Renderer {
static renderTemplate(template: string, vars: TemplateVariables | SnippetVariables) {
static renderTemplate(template: string, vars: TemplateVariables) {
if (TYPOGRAF_LOCALES.has(vars.locale)) {
const tp = new Typograf({ locale: [TYPOGRAF_LOCALES.get(vars.locale)], htmlEntity: { type: "name" } });
for (const prop in vars) {
vars[prop] = tp.execute(vars[prop]);
}
}
return render(template, vars, null, { escape: (str) => str });
}

static renderSnippet(template: string, vars: SnippetVariables) {
return render(template, vars);
}
}
7 changes: 7 additions & 0 deletions lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@ export const DEFAULTS: Defaults = {
};

export const MANDATORY_CONFIG_PROPS = ["locale", "theme", "tailwind"];

// Mapping between locales supported by the Typograf and this tool src locales
export const TYPOGRAF_LOCALES = new Map([
["de", "de"],
["en", "en-US"],
["ru", "ru"],
]);
32 changes: 23 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "error-pages",
"version": "1.8.5",
"version": "1.9.0",
"description": "Lightweight tool to create static HTTP Error Pages in minimalistic adaptive and accessible design with customization and localization support",
"main": "index.ts",
"scripts": {
Expand All @@ -25,7 +25,8 @@
"reflect-metadata": "^0.1.13",
"tailwindcss": "^3.2.4",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
"typescript": "^4.9.4",
"typograf": "^7.1.0"
},
"devDependencies": {
"@commitlint/cli": "^17.4.2",
Expand Down
48 changes: 48 additions & 0 deletions test/Renderer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { assert } from "chai";

import { Renderer } from "../lib/classes/Renderer";
import { SnippetVariables, TemplateVariables } from "../lib/interfaces";

describe("class Renderer", () => {
describe("renderTemplate()", () => {
let template: string, vars: TemplateVariables;

beforeEach(() => {
template = "<p>{{ myText }}</p>";
vars = {
locale: "en",
version: "0.0.0",
myText: "This text is for test",
};
});

it("should be rendered regular html with typographed text", () => {
const result = Renderer.renderTemplate(template, vars);
const expected = "<p>This text is&nbsp;for test</p>";
assert.equal(result, expected);
});

it("should be rendered regular html without typography", () => {
const result = Renderer.renderTemplate(template, { ...vars, locale: "xz" });
const expected = "<p>This text is for test</p>";
assert.equal(result, expected);
});
});

describe("renderSnippet()", () => {
let template: string, vars: SnippetVariables;

beforeEach(() => {
template = "{{#codes}}{{.}}, {{/codes}}";
vars = {
codes: [1, 2, 3],
};
});

it("should be rendered part of config", () => {
const result = Renderer.renderSnippet(template, vars);
const expected = "1, 2, 3, ";
assert.equal(result, expected);
});
});
});
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"compilerOptions": {
"typeRoots": ["./node_modules/@types"],
"experimentalDecorators": true,
"emitDecoratorMetadata": true
"emitDecoratorMetadata": true,
"esModuleInterop": true
}
}

0 comments on commit 71b61c7

Please sign in to comment.