Skip to content

Commit

Permalink
feat: make hooks processMarkdown, processHTML async
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Mar 23, 2019
1 parent 13a7ab0 commit c069379
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@
],
"rules": {
"unicorn/filename-case": "off",
"unicorn/no-abusive-eslint-disable": "off"
"unicorn/no-abusive-eslint-disable": "off",
"require-atomic-updates": "off"
}
},
"husky": {
Expand Down
9 changes: 9 additions & 0 deletions src/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ class Hooks {
}
return arg
}

async processPromise(name, arg) {
const hooks = this.hooks[name] || []
for (const fn of hooks) {
// eslint-disable-next-line no-await-in-loop
arg = (await fn(arg)) || arg
}
return arg
}
}

export default new Hooks()
7 changes: 3 additions & 4 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,8 @@ const store = new Vuex.Store({
dispatch('fetchPrismLanguages')
])

// TODO: remove processMarkdown hook
page.content = hooks.process('processMarkdown', page.content)
page = hooks.process('processPage', page)
page.content = await hooks.processPromise('processMarkdown', page.content)
page = await hooks.processPromise('processPage', page)

const env = {
headings: [],
Expand All @@ -101,7 +100,7 @@ const store = new Vuex.Store({
env
})
}
page.content = hooks.process('processHTML', page.content)
page.content = await hooks.processPromise('processHTML', page.content)
page.headings = env.headings
if (!page.title) {
page.title = env.title
Expand Down
4 changes: 2 additions & 2 deletions website/docs/plugin-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ Plugin properties:

## api.processMarkdown(fn)

- `fn`: `(text: string) => string`
- `fn`: `(text: string) => string | Promise<string>`

Process markdown.

## api.processHTML(fn)

- `fn`: `(html: string) => string`
- `fn`: `(html: string) => string | Promise <string>`

Process HTML.

Expand Down
4 changes: 2 additions & 2 deletions website/docs/zh/plugin-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

## api.processMarkdown(fn)

- `fn``(text: string) => string`
- `fn``(text: string) => string | Promise <string>`

处理 markdown。

Expand All @@ -19,7 +19,7 @@

## api.processHTML(fn)

- `fn``(html: string) => string`
- `fn``(html: string) => string | Promise <string>`

处理 HTML.

Expand Down

0 comments on commit c069379

Please sign in to comment.