Skip to content

Commit

Permalink
Merge pull request #1318 from maizzle/events-posthtml
Browse files Browse the repository at this point in the history
  • Loading branch information
cossssmin authored Jul 30, 2024
2 parents 657cba3 + 50eb0ff commit b62c762
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 38 deletions.
9 changes: 0 additions & 9 deletions src/generators/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,13 @@ export async function render(html = '', config = {}) {
* @param {string} options.html - The HTML to be transformed
* @param {Object} options.matter - The front matter data
* @param {Object} options.config - The current template config
* @param {function} options.posthtml - The PostHTML compiler
* @param {Object} options.transform - The transformers object
* @returns {string} - The transformed HTML, or the original one if nothing was returned
*/
if (typeof templateConfig.beforeRender === 'function') {
content = await templateConfig.beforeRender(({
html: content,
matter: matterData,
config: templateConfig,
posthtml: compilePostHTML,
})) ?? content
}

Expand All @@ -83,16 +80,13 @@ export async function render(html = '', config = {}) {
* @param {string} options.html - The HTML to be transformed
* @param {Object} options.matter - The front matter data
* @param {Object} options.config - The current template config
* @param {function} options.posthtml - The PostHTML compiler
* @param {Object} options.transform - The transformers object
* @returns {string} - The transformed HTML, or the original one if nothing was returned
*/
if (typeof templateConfig.afterRender === 'function') {
compiled.html = await templateConfig.afterRender(({
html: compiled.html,
matter: matterData,
config: templateConfig,
posthtml: compilePostHTML,
})) ?? compiled.html
}

Expand All @@ -118,16 +112,13 @@ export async function render(html = '', config = {}) {
* @param {string} options.html - The HTML to be transformed
* @param {Object} options.matter - The front matter data
* @param {Object} options.config - The current template config
* @param {function} options.posthtml - The PostHTML compiler
* @param {Object} options.transform - The transformers object
* @returns {string} - The transformed HTML, or the original one if nothing was returned
*/
if (typeof templateConfig.afterTransformers === 'function') {
compiled.html = await templateConfig.afterTransformers(({
html: compiled.html,
matter: matterData,
config: templateConfig,
posthtml: compilePostHTML,
})) ?? compiled.html
}

Expand Down
12 changes: 6 additions & 6 deletions test/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ describe.concurrent('Render', () => {

test('Runs the `beforeRender` event', async () => {
const { html } = await render('<div class="inline">{{ page.foo }}</div>', {
beforeRender({ config, posthtml }) {
beforeRender({ config, matter }) {
config.foo = 'bar'

expect(config).toBeInstanceOf(Object)
expect(posthtml).toBeInstanceOf(Function)
expect(matter).toBeInstanceOf(Object)
}
})

Expand All @@ -41,13 +41,13 @@ describe.concurrent('Render', () => {

test('Runs the `afterRender` event', async () => {
const { html } = await render('<div class="inline">foo</div>', {
afterRender({ config, posthtml }) {
afterRender({ config, matter }) {
config.replaceStrings = {
foo: 'bar'
}

expect(config).toBeInstanceOf(Object)
expect(posthtml).toBeInstanceOf(Function)
expect(matter).toBeInstanceOf(Object)
}
})

Expand All @@ -59,9 +59,9 @@ describe.concurrent('Render', () => {
replaceStrings: {
foo: 'bar'
},
afterTransformers({ html, config, posthtml }) {
afterTransformers({ html, matter, config }) {
expect(config).toBeInstanceOf(Object)
expect(posthtml).toBeInstanceOf(Function)
expect(matter).toBeInstanceOf(Object)

return html.replace('bar', 'baz')
}
Expand Down
23 changes: 0 additions & 23 deletions types/events.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type Config from "./config";

type PostHTMLType = (html: string, config: Config) => { html: string; config: Config };

export default interface Events {
/**
* Runs after the Environment config has been computed, but before Templates are processed.
Expand Down Expand Up @@ -57,13 +55,6 @@ export default interface Events {
* This is the Environment config merged with the Template's Front Matter.
*/
config: Config;
/**
* A function to process an HTML string with PostHTML.
*
* @param {string} html The HTML string to process.
* @param {Config} config The Maizzle config object.
*/
posthtml: PostHTMLType;
}) => string | Promise<string>;

/**
Expand Down Expand Up @@ -98,13 +89,6 @@ export default interface Events {
* This is the Environment config merged with the Template's Front Matter.
*/
config: Config;
/**
* A function to process an HTML string with PostHTML.
*
* @param {string} html The HTML string to process.
* @param {Config} config The Maizzle config object.
*/
posthtml: PostHTMLType;
}) => string | Promise<string>;

/**
Expand Down Expand Up @@ -139,13 +123,6 @@ export default interface Events {
* This is the Environment config merged with the Template's Front Matter.
*/
config: Config;
/**
* A function to process an HTML string with PostHTML.
*
* @param {string} html The HTML string to process.
* @param {Config} config The Maizzle config object.
*/
posthtml: PostHTMLType;
}) => string | Promise<string>;

/**
Expand Down

0 comments on commit b62c762

Please sign in to comment.