-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add automated typography support (#22)
* feat: add automated typography support * fix: lint * 1.9.0
- Loading branch information
Showing
8 changed files
with
102 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters