Skip to content

Commit

Permalink
changed request parameters to be an object instead of individual values.
Browse files Browse the repository at this point in the history
Updated documentation
Reformat code!
  • Loading branch information
awaragi committed Apr 17, 2024
1 parent b567eba commit ae1accd
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 131 deletions.
30 changes: 26 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <http://plantuml.com/plantuml> 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

Expand Down
8 changes: 2 additions & 6 deletions lib/eleventy-plugin-plantuml.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ const defaultOptions = {
prefix: "/plantuml",
outputType: "png",
imgClass: "plantuml",
requestTimeout: undefined,
requestSocketTimeout: undefined,
request: undefined,
};

function generatePlantumlUrl(encoded, options) {
Expand All @@ -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());
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]:awaragi/eleventy-plugin-plantuml.git",
Expand Down
257 changes: 137 additions & 120 deletions test/eleventy-plugin-plantuml.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
`<h1>${title}</h1><p>${content}</p>`,
(err) => {
if (err) throw err;
}
);
}
// append content to the end of the file /tmp/eleventy-plugin-plantuml-test.html
fs.appendFileSync(
previewFile,
`<h1>${title}</h1><p>${content}</p>`,
(err) => {
if (err) throw err;
}
);
};

describe("Eleventy PlantUML Test Suite", () => {
beforeAll(() => {
// create the file /tmp/eleventy-plugin-plantuml-test.html
fs.writeFileSync(previewFile, "<html lang='en'><body>");
});

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, "<html lang='en'><body>");
});

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('<img class="plantuml" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA')).toBeTruthy();
});

it("Testing a e2e large PNG conversion", () => {
const img = eleventyPluginPlantuml.highlight(
`@startuml
eleventyPluginPlantuml.defaultOptions
);
appendContent("basic PNG conversion", img);
expect(
img.startsWith(
'<img class="plantuml" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA'
)
).toBeTruthy();
});

it("Testing a e2e large PNG conversion", () => {
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
Expand All @@ -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('<img class="plantuml" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA')).toBeTruthy();
});

it("Testing a e2e basic SVG conversion", () => {
const img = eleventyPluginPlantuml.highlight(
`@startuml
eleventyPluginPlantuml.defaultOptions
);
appendContent("large PNG conversion", img);
expect(
img.startsWith(
'<img class="plantuml" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA'
)
).toBeTruthy();
});

it("Testing a e2e basic SVG conversion", () => {
const img = eleventyPluginPlantuml.highlight(
`@startuml
Bob -> Alice : hello
@enduml`,
Object.assign({}, eleventyPluginPlantuml.defaultOptions, {
outputType: "svg",
})
);
appendContent("basic SVG conversion", img);
expect(img.startsWith('<?xml version="1.0" encoding="us-ascii" standalone="no"?><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" contentStyleType="text/css" ')).toBeTruthy();
});

it("Testing a e2e large SVG conversion", () => {
const img = eleventyPluginPlantuml.highlight(
`@startuml
Object.assign({}, eleventyPluginPlantuml.defaultOptions, {
outputType: "svg",
})
);
appendContent("basic SVG conversion", img);
expect(
img.startsWith(
'<?xml version="1.0" encoding="us-ascii" standalone="no"?><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" contentStyleType="text/css" '
)
).toBeTruthy();
});

it("Testing a e2e large SVG conversion", () => {
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
Expand All @@ -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('<?xml version="1.0" encoding="us-ascii" standalone="no"?><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" contentStyleType="text/css" ')).toBeTruthy();
});

it("Testing e2e conversion w/txt output type", () => {
const img = eleventyPluginPlantuml.highlight(
`@startuml
Object.assign({}, eleventyPluginPlantuml.defaultOptions, {
outputType: "svg",
})
);
appendContent("large SVG conversion", img);
expect(
img.startsWith(
'<?xml version="1.0" encoding="us-ascii" standalone="no"?><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" contentStyleType="text/css" '
)
).toBeTruthy();
});

it("Testing e2e conversion w/txt output type", () => {
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(
'<img class="plantuml" src="data:image/png;base64,ABC" alt="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(
'<img class="plantuml" src="data:image/png;base64,ABC" alt="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);
}
});
});

0 comments on commit ae1accd

Please sign in to comment.