@lowlighter/markdown 1.0.0 → 1.2.0

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/renderer.ts CHANGED
@@ -32,9 +32,13 @@ export class Renderer {
32
32
  * @example
33
33
  * ```ts
34
34
  * import { Renderer } from "./renderer.ts"
35
- * import pluginGfm from "./plugins/gfm.ts"
36
- *
37
- * const renderer = new Renderer({ plugins: [ pluginGfm ] })
35
+ * await Renderer.render("# Hello, world!")
36
+ * ```
37
+ * @example
38
+ * ```ts
39
+ * import { Renderer } from "./renderer.ts"
40
+ * import { gfm, highlighting, math, markers, wikilinks, sanitize } from "./plugins/mod.ts"
41
+ * const renderer = new Renderer({ plugins: [ gfm, highlighting, math, markers, wikilinks, sanitize ] })
38
42
  * await renderer.render("# Hello, world!")
39
43
  * ```
40
44
  */
@@ -103,9 +107,19 @@ export class Renderer {
103
107
  * await renderer.render("# foo")
104
108
  * ```
105
109
  */
106
- static async with({ plugins = [] }: { plugins: Array<Plugin | URL | string> }): Promise<Renderer> {
110
+ static async with({ plugins = [], throw: throws = true }: { plugins: Array<Plugin | URL | string>; throw?: boolean }): Promise<Renderer> {
107
111
  plugins = plugins.map((plugin) => plugin instanceof URL ? plugin.href : plugin)
108
- const resolved = await Promise.all(plugins.map((plugin) => (typeof plugin === "string") ? import(plugin) : plugin))
112
+ const imported = await Promise.allSettled(plugins.map((plugin) => (typeof plugin === "string") ? import(plugin) : plugin))
113
+ const resolved = imported
114
+ .filter((result): result is PromiseFulfilledResult<Plugin> => result.status === "fulfilled")
115
+ .map(({ value }) => value)
116
+ if (throws && (imported.some(({ status }) => status === "rejected"))) {
117
+ const errors = imported
118
+ .filter((result): result is PromiseRejectedResult => result.status === "rejected")
119
+ .map(({ reason }) => reason)
120
+ .join(", ")
121
+ throw new ReferenceError(`Failed to import some plugins: ${errors}`)
122
+ }
109
123
  return new Renderer({ plugins: resolved })
110
124
  }
111
125
  }
package/renderer_test.ts CHANGED
@@ -12,3 +12,8 @@ test("deno")("Renderer.with() instantiates a new customized renderer", async ()
12
12
  const markdown = await Renderer.with({ plugins: ["./plugins/gfm.ts", new URL("./plugins/gfm.ts", import.meta.url), pluginGfm] })
13
13
  await expect(markdown.render("# foo")).resolves.toBe("<h1>foo</h1>")
14
14
  })
15
+
16
+ test("deno")("Renderer.with() honors throw option when creating a new customized renderer", async () => {
17
+ await expect(Renderer.with({ plugins: ["unknown"], throw: false })).resolves.toBeInstanceOf(Renderer)
18
+ await expect(Renderer.with({ plugins: ["unknown"], throw: true })).rejects.toThrow(ReferenceError)
19
+ })