-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed default exposts to only include plugin feature
- Loading branch information
Pierre Awaragi
committed
Jun 27, 2021
1 parent
f25c325
commit dc59529
Showing
10 changed files
with
1,402 additions
and
1,359 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,6 @@ test/ | |
jest.config.js | ||
yarn.lock | ||
yarn-error.log | ||
/diagram.png | ||
/diagram.png | ||
.prettierignore | ||
.prettierrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/* |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.