Skip to content

Commit

Permalink
added prettier
Browse files Browse the repository at this point in the history
fixed default exposts to only include plugin feature
  • Loading branch information
Pierre Awaragi committed Jun 27, 2021
1 parent f25c325 commit dc59529
Show file tree
Hide file tree
Showing 10 changed files with 1,402 additions and 1,359 deletions.
4 changes: 3 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ test/
jest.config.js
yarn.lock
yarn-error.log
/diagram.png
/diagram.png
.prettierignore
.prettierrc
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/*
Empty file added .prettierrc
Empty file.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ plantuml to an inline dataurl png ```<img>```
* [eleventy](https://www.npmjs.com/package/@11ty/eleventy) A simpler static site generator for which this plugin is make.
* [sync-request](https://www.npmjs.com/package/sync-request) (for making blocking synchronous http request - not to be used in production)
* [jest](https://www.npmjs.com/package/jest) (for executing unit tests)
* [prettier](https://www.npmjs.com/package/prettier) (for keeping things clean and tidy)

## Installation

Expand Down Expand Up @@ -58,15 +59,17 @@ plugin -> MDH: img src="dataurl"
`` `
```

The result is the following image inserted (inline) in the generated html site
The result is an image inserted (inline) in the generated html site (click to open)

![sequence diagram](./diagram.png)
[Sequence diagram](https://github.com/awaragi/eleventy-plugin-plantuml/blob/master/diagram.png)

## Contribute
* create a fork of this repo
* make your changes
* run prettier for code format consistency
* test your code by running ```yarn test``` or ```npm test```
* create a Pull Request
* Send me an email in case I miss the PR notification


## Testing
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('./lib/eleventy-plugin-plantuml');
module.exports = { plugin: require('./lib/eleventy-plugin-plantuml')};
98 changes: 50 additions & 48 deletions lib/eleventy-plugin-plantuml.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,71 @@
const request = require('sync-request');
const plantumlEncode = require('./plantumlEncode');
const request = require("sync-request");
const plantumlEncode = require("./plantumlEncode");

const defaultOptions = {
protocol: 'http',
hostname: 'www.plantuml.com',
port: "80",
prefix: "/plantuml",
outputType: "png",
imgClass: "plantuml"
protocol: "http",
hostname: "www.plantuml.com",
port: "80",
prefix: "/plantuml",
outputType: "png",
imgClass: "plantuml",
};

function generatePlantumlUrl(encoded, options) {
return `${options.protocol}://${options.hostname}${options.port === 80 ? "" : options.port === 443 ? "" : (":" + options.port)}${options.prefix}/${options.outputType}/${encoded}`;
return `${options.protocol}://${options.hostname}${
options.port === 80 ? "" : options.port === 443 ? "" : ":" + options.port
}${options.prefix}/${options.outputType}/${encoded}`;
}

const generateImageAsBase64String = (url, options) => {
// request url
const response = request('GET', url);
// convert from Uint8Array to buffer to base64 string
return Buffer.from(response.getBody()).toString('base64');
}
// request url
const response = request("GET", url);
// convert from Uint8Array to buffer to base64 string
return Buffer.from(response.getBody()).toString("base64");
};

const generateImgTag = (imageBase64, options) => {
return `<img class="${options.imgClass}" src="data:image/png;base64,${imageBase64}" alt="Plantuml Diagram" />`;
return `<img class="${options.imgClass}" src="data:image/png;base64,${imageBase64}" alt="Plantuml Diagram" />`;
};

const highlight = (diagram, options) => {
// compute compressed version of str
const encoded = plantumlEncode(diagram);
// URL to send to plantuml for conversion
const url = generatePlantumlUrl(encoded, options);
// Call plantuml server to generate image
const imageBase64 = generateImageAsBase64String(url, options);
// Finally convert image to dataurl
return generateImgTag(imageBase64, options);
// compute compressed version of str
const encoded = plantumlEncode(diagram);
// URL to send to plantuml for conversion
const url = generatePlantumlUrl(encoded, options);
// Call plantuml server to generate image
const imageBase64 = generateImageAsBase64String(url, options);
// Finally convert image to dataurl
return generateImgTag(imageBase64, options);
};

const plugin = (eleventyConfig, pluginOptions = {}) => {
// default options
const options = Object.assign({}, defaultOptions, pluginOptions);
// default options
const options = Object.assign({}, defaultOptions, pluginOptions);

// preserve chain of highlighter
const highlighter = eleventyConfig.markdownHighlighter;
// preserve chain of highlighter
const highlighter = eleventyConfig.markdownHighlighter;

// add highlighter
eleventyConfig.addMarkdownHighlighter((diagram, language) => {
if (language === "plantuml") {
return highlight(diagram, options);
}
// just in case highlighter is not enabled in which case I am not sure why we are doing
if (highlighter) {
return highlighter(diagram, language)
}
// default highlighter just in case
return `<pre class="${language}">${diagram}</a>`;
});
return {}
// add highlighter
eleventyConfig.addMarkdownHighlighter((diagram, language) => {
if (language === "plantuml") {
return highlight(diagram, options);
}
// just in case highlighter is not enabled in which case I am not sure why we are doing
if (highlighter) {
return highlighter(diagram, language);
}
// default highlighter just in case
return `<pre class="${language}">${diagram}</a>`;
});
return {};
};

module.exports = {
defaultOptions,
plugin,
highlight,
generatePlantumlUrl,
generateImageAsBase64String,
generateImgTag
};
defaultOptions,
plugin,
highlight,

generatePlantumlUrl,
generateImageAsBase64String,
generateImgTag,
};
97 changes: 50 additions & 47 deletions lib/plantumlEncode.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,60 @@
* Original:
* https://github.com/johan/js-deflate
*/
const { deflate } = require('./rawdeflate');
const { deflate } = require("./rawdeflate");

const encode64 = data => {
let r = "";
for (let i=0; i<data.length; i+=3) {
if (i+2===data.length) {
r +=append3bytes(data.charCodeAt(i), data.charCodeAt(i+1), 0);
} else if (i+1===data.length) {
r += append3bytes(data.charCodeAt(i), 0, 0);
} else {
r += append3bytes(data.charCodeAt(i), data.charCodeAt(i+1),
data.charCodeAt(i+2));
}
const encode64 = (data) => {
let r = "";
for (let i = 0; i < data.length; i += 3) {
if (i + 2 === data.length) {
r += append3bytes(data.charCodeAt(i), data.charCodeAt(i + 1), 0);
} else if (i + 1 === data.length) {
r += append3bytes(data.charCodeAt(i), 0, 0);
} else {
r += append3bytes(
data.charCodeAt(i),
data.charCodeAt(i + 1),
data.charCodeAt(i + 2)
);
}
return r;
}
}
return r;
};

const append3bytes = (b1, b2, b3) => {
let c1 = b1 >> 2;
let c2 = ((b1 & 0x3) << 4) | (b2 >> 4);
let c3 = ((b2 & 0xF) << 2) | (b3 >> 6);
let c4 = b3 & 0x3F;
let r = "";
r += encode6bit(c1 & 0x3F);
r += encode6bit(c2 & 0x3F);
r += encode6bit(c3 & 0x3F);
r += encode6bit(c4 & 0x3F);
return r;
}
let c1 = b1 >> 2;
let c2 = ((b1 & 0x3) << 4) | (b2 >> 4);
let c3 = ((b2 & 0xf) << 2) | (b3 >> 6);
let c4 = b3 & 0x3f;
let r = "";
r += encode6bit(c1 & 0x3f);
r += encode6bit(c2 & 0x3f);
r += encode6bit(c3 & 0x3f);
r += encode6bit(c4 & 0x3f);
return r;
};

const encode6bit = b => {
if (b < 10) {
return String.fromCharCode(48 + b);
}
b -= 10;
if (b < 26) {
return String.fromCharCode(65 + b);
}
b -= 26;
if (b < 26) {
return String.fromCharCode(97 + b);
}
b -= 26;
if (b === 0) {
return '-';
}
if (b === 1) {
return '_';
}
return '?';
}
const encode6bit = (b) => {
if (b < 10) {
return String.fromCharCode(48 + b);
}
b -= 10;
if (b < 26) {
return String.fromCharCode(65 + b);
}
b -= 26;
if (b < 26) {
return String.fromCharCode(97 + b);
}
b -= 26;
if (b === 0) {
return "-";
}
if (b === 1) {
return "_";
}
return "?";
};
const geturl = (s) => encode64(deflate(unescape(encodeURIComponent(s)), 9));

module.exports = geturl;
module.exports = geturl;
Loading

0 comments on commit dc59529

Please sign in to comment.