@lowlighter/markdown 1.1.0 → 1.2.1

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
@@ -107,9 +107,19 @@ export class Renderer {
107
107
  * await renderer.render("# foo")
108
108
  * ```
109
109
  */
110
- 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> {
111
111
  plugins = plugins.map((plugin) => plugin instanceof URL ? plugin.href : plugin)
112
- 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
+ }
113
123
  return new Renderer({ plugins: resolved })
114
124
  }
115
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
+ })