From ae1accd71ea812f752e3fbdb613f86c03060b45b Mon Sep 17 00:00:00 2001 From: Pierre Awaragi Date: Wed, 17 Apr 2024 08:46:20 -0400 Subject: [PATCH] changed request parameters to be an object instead of individual values. Updated documentation Reformat code! --- README.md | 30 ++- lib/eleventy-plugin-plantuml.js | 8 +- package.json | 2 +- test/eleventy-plugin-plantuml.test.js | 257 ++++++++++++++------------ 4 files changed, 166 insertions(+), 131 deletions(-) diff --git a/README.md b/README.md index 9850904..006cf05 100644 --- a/README.md +++ b/README.md @@ -34,17 +34,39 @@ eleventyConfig.addPlugin(plantuml.plugin, { port: 8888, prefix: "", outputType: "svg", - imgClass: "plantuml", - requestTimeout: undefined, // undefined or millisecondes - requestSocketTimeout: undefined, // undefined or millisecondes + imgClass: "plantuml" }); ``` If the server options are omitted, the plugin defaults to server for conversion, and to PNG for output type. +### Styling + By default the generated img tag will have class **plantuml** assigned to it. This can be overridden using options.imgClass value. -Timeout options are passed directly to the underlying request object. See https://www.npmjs.com/package/sync-request. They default to sync-request +### Request settings + +In the rare case where control of the http request settings is required, an optional setting "request" can be added to the plugin config object which will be passed directly to the underlying request object. See https://www.npmjs.com/package/sync-request for all available settings. + +```javascript +const plantuml = require('eleventy-plugin-plantuml'); +eleventyConfig.addPlugin(plantuml.plugin, { + protocol: "http", + hostname: "localhost", + port: 8888, + prefix: "", + outputType: "svg", + imgClass: "plantuml", + request: { + timeout: ... + socketTimeout: .... + retry: ... + retryDelay: ... + maxRetries: ... + } +}); + +``` ## Using in templates diff --git a/lib/eleventy-plugin-plantuml.js b/lib/eleventy-plugin-plantuml.js index cde8fd1..7fd45d0 100644 --- a/lib/eleventy-plugin-plantuml.js +++ b/lib/eleventy-plugin-plantuml.js @@ -8,8 +8,7 @@ const defaultOptions = { prefix: "/plantuml", outputType: "png", imgClass: "plantuml", - requestTimeout: undefined, - requestSocketTimeout: undefined, + request: undefined, }; function generatePlantumlUrl(encoded, options) { @@ -20,10 +19,7 @@ function generatePlantumlUrl(encoded, options) { const fetchImage = (url, options) => { // request url - const response = request("GET", url, { - timeout: options.requestTimeout, - socketTimeout: options.requestSocketTimeout, - }); + const response = request("GET", url, options.request); // convert from Uint8Array to buffer return Buffer.from(response.getBody()); }; diff --git a/package.json b/package.json index 506b6b3..6c1ee2c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "eleventy-plugin-plantuml", - "version": "1.1.6", + "version": "1.1.7", "description": "Eleventy (11ty) Plantuml markdown plugin", "main": "index.js", "repository": "git@github.com:awaragi/eleventy-plugin-plantuml.git", diff --git a/test/eleventy-plugin-plantuml.test.js b/test/eleventy-plugin-plantuml.test.js index 9298f39..6f4c1ac 100644 --- a/test/eleventy-plugin-plantuml.test.js +++ b/test/eleventy-plugin-plantuml.test.js @@ -5,36 +5,40 @@ const eleventyPluginPlantuml = require("../lib/eleventy-plugin-plantuml"); const previewFile = "/tmp/eleventy-plugin-plantuml-test.html"; appendContent = (title, content) => { - // append content to the end of the file /tmp/eleventy-plugin-plantuml-test.html - fs.appendFileSync( - previewFile, - `

${title}

${content}

`, - (err) => { - if (err) throw err; - } - ); -} + // append content to the end of the file /tmp/eleventy-plugin-plantuml-test.html + fs.appendFileSync( + previewFile, + `

${title}

${content}

`, + (err) => { + if (err) throw err; + } + ); +}; describe("Eleventy PlantUML Test Suite", () => { - beforeAll(() => { - // create the file /tmp/eleventy-plugin-plantuml-test.html - fs.writeFileSync(previewFile, ""); - }); - - test("Testing a e2e basic PNG conversion", () => { - const img = eleventyPluginPlantuml.highlight( - `@startuml + beforeAll(() => { + // create the file /tmp/eleventy-plugin-plantuml-test.html + fs.writeFileSync(previewFile, ""); + }); + + test("Testing a e2e basic PNG conversion", () => { + const img = eleventyPluginPlantuml.highlight( + `@startuml Bob -> Alice : hello @enduml`, - eleventyPluginPlantuml.defaultOptions - ); - appendContent("basic PNG conversion", img); - expect(img.startsWith(' { - const img = eleventyPluginPlantuml.highlight( - `@startuml + eleventyPluginPlantuml.defaultOptions + ); + appendContent("basic PNG conversion", img); + expect( + img.startsWith( + ' { + const img = eleventyPluginPlantuml.highlight( + `@startuml !include https://raw.githubusercontent.com/bschwarz/puml-themes/master/themes/bluegray/puml-theme-bluegray.puml participant "Makrdown Highlighter" as MDH participant "eleventy-plugin-plantumt" as plugin @@ -47,28 +51,36 @@ plantuml -> plugin: image/png plugin -> plugin: base64 plugin -> MDH: img src="dataurl" @enduml`, - eleventyPluginPlantuml.defaultOptions - ); - appendContent("large PNG conversion", img); - expect(img.startsWith(' { - const img = eleventyPluginPlantuml.highlight( - `@startuml + eleventyPluginPlantuml.defaultOptions + ); + appendContent("large PNG conversion", img); + expect( + img.startsWith( + ' { + const img = eleventyPluginPlantuml.highlight( + `@startuml Bob -> Alice : hello @enduml`, - Object.assign({}, eleventyPluginPlantuml.defaultOptions, { - outputType: "svg", - }) - ); - appendContent("basic SVG conversion", img); - expect(img.startsWith(' { - const img = eleventyPluginPlantuml.highlight( - `@startuml + Object.assign({}, eleventyPluginPlantuml.defaultOptions, { + outputType: "svg", + }) + ); + appendContent("basic SVG conversion", img); + expect( + img.startsWith( + ' { + const img = eleventyPluginPlantuml.highlight( + `@startuml !include https://raw.githubusercontent.com/bschwarz/puml-themes/master/themes/bluegray/puml-theme-bluegray.puml participant "Makrdown Highlighter" as MDH participant "eleventy-plugin-plantumt" as plugin @@ -81,86 +93,91 @@ plantuml -> plugin: image/png plugin -> plugin: base64 plugin -> MDH: img src="dataurl" @enduml`, - Object.assign({}, eleventyPluginPlantuml.defaultOptions, { - outputType: "svg", - }) - ); - appendContent("large SVG conversion", img); - expect(img.startsWith(' { - const img = eleventyPluginPlantuml.highlight( - `@startuml + Object.assign({}, eleventyPluginPlantuml.defaultOptions, { + outputType: "svg", + }) + ); + appendContent("large SVG conversion", img); + expect( + img.startsWith( + ' { + const img = eleventyPluginPlantuml.highlight( + `@startuml Bob -> Alice : hello @enduml`, - Object.assign({}, eleventyPluginPlantuml.defaultOptions, { - outputType: "txt", - }) - ); - expect(img).toBe(""); - }); + Object.assign({}, eleventyPluginPlantuml.defaultOptions, { + outputType: "txt", + }) + ); + expect(img).toBe(""); + }); - it("Testing e2e conversion w/invalid output type", () => { - const img = eleventyPluginPlantuml.highlight( - `@startuml + it("Testing e2e conversion w/invalid output type", () => { + const img = eleventyPluginPlantuml.highlight( + `@startuml Bob -> Alice : hello @enduml`, - Object.assign({}, eleventyPluginPlantuml.defaultOptions, { - outputType: "asdf", - }) - ); - expect(img).toBe(""); - }); - - it("Testing url generation", () => { - const url = eleventyPluginPlantuml.generatePlantumlUrl("ABC", { - protocol: "https", - hostname: "localhost", - port: "8080", - prefix: "/path", - outputType: "png", - }); - expect(url).toBe("https://localhost:8080/path/png/ABC"); - }); - - it("Testing img tag generation", () => { - const tag = eleventyPluginPlantuml.generateImgTag("ABC", { - imgClass: "plantuml", - }); - expect(tag).toBe( - 'Plantuml Diagram' - ); - }); - - it("Test request timeout", () => { - try { - eleventyPluginPlantuml.highlight( - `@startuml @enduml`, - Object.assign({}, eleventyPluginPlantuml.defaultOptions, { - requestTimeout: 1, - }) - ); - // should not get here - expect(false).beTruthy(); - } catch (e) { - // test passed - expect(e).toBeInstanceOf(Error); } + Object.assign({}, eleventyPluginPlantuml.defaultOptions, { + outputType: "asdf", + }) + ); + expect(img).toBe(""); + }); + + it("Testing url generation", () => { + const url = eleventyPluginPlantuml.generatePlantumlUrl("ABC", { + protocol: "https", + hostname: "localhost", + port: "8080", + prefix: "/path", + outputType: "png", }); + expect(url).toBe("https://localhost:8080/path/png/ABC"); + }); - it("Test request socket timeout", () => { - try { - eleventyPluginPlantuml.highlight( - `@startuml @enduml`, - Object.assign({}, eleventyPluginPlantuml.defaultOptions, { - requestSocketTimeout: 1, - }) - ); - // should not get here - expect(false).beTruthy(); - } catch (e) { - // test passed - expect(e).toBeInstanceOf(Error); - } + it("Testing img tag generation", () => { + const tag = eleventyPluginPlantuml.generateImgTag("ABC", { + imgClass: "plantuml", }); + expect(tag).toBe( + 'Plantuml Diagram' + ); + }); + + it("Test request timeout", () => { + try { + eleventyPluginPlantuml.highlight( + `@startuml @enduml`, + Object.assign({}, eleventyPluginPlantuml.defaultOptions, { + request: { Timeout: 1 }, + }) + ); + // should not get here + expect(false).beTruthy(); + } catch (e) { + // test passed + expect(e).toBeInstanceOf(Error); + } + }); + + it("Test request socket timeout", () => { + try { + eleventyPluginPlantuml.highlight( + `@startuml @enduml`, + Object.assign({}, eleventyPluginPlantuml.defaultOptions, { + request: { SocketTimeout: 1 }, + }) + ); + // should not get here + expect(false).beTruthy(); + } catch (e) { + // test passed + expect(e).toBeInstanceOf(Error); + } + }); });