puml-for-markdown
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -41,9 +41,8 @@ #!/usr/bin/env node | ||
.option( | ||
'-g, --respect-gitignore', | ||
'Automatically ignore MD files in .gitignore paths', | ||
true, | ||
'-g, --ignore-gitignore', | ||
"Don't ignore files PUML and MD files in projects gitignore", | ||
) | ||
.addOption(new Option( | ||
'-i, --gitignore-path <path>', | ||
'If --respect-gitignore is set, use set this as path to .gitignore file.' | ||
'Use this as path to .gitignore file.' | ||
).default(false, 'rootDirectory/.gitignore') | ||
@@ -74,7 +73,6 @@ ) | ||
opts.pumlDirectory = opts.pumlDirectory || opts.rootDirectory | ||
if (opts.imageFormats === 'both') { | ||
opts.imageFormats = ['png', 'svg'] | ||
} else { | ||
opts.imageFormats = [opts.imageFormats] | ||
} | ||
opts.shouldShortenLinks = !opts.turnOffLinkShortening | ||
opts.respectGitignore = !opts.ignoreGitignore | ||
opts.imageFormats = opts.imageFormats === 'both' ? ['png', 'svg'] : [opts.imageFormats] | ||
return run(opts).catch((e) => { | ||
@@ -81,0 +79,0 @@ console.error('FATAL EXCEPTION') |
55
index.js
@@ -22,4 +22,3 @@ /** | ||
const PUML_SVG_SERVER = 'https://www.plantuml.com/plantuml/svg' | ||
const PUML_PNG_SERVER = 'https://www.plantuml.com/plantuml/png' | ||
const PUML_SERVER = 'https://www.plantuml.com/plantuml' | ||
@@ -30,2 +29,5 @@ // HELPERS | ||
const mkdirIfDoesntExist = (p) => !fs.existsSync(p) && fs.mkdirSync(p, {recursive: true}) | ||
const getPumlUrl = (imgFormat, encodedData, shorten) => shorten ? | ||
tiny.shorten(getFullPumlUrl(imgFormat, encodedData)) : getFullPumlUrl(imgFormat, encodedData) | ||
const getFullPumlUrl = (imgFormat, encodedData) => `${PUML_SERVER}/${imgFormat}/${encodedData}}` | ||
const mapUniqMatches = (s, re, mapper, match = re.exec(s), results = []) => { | ||
@@ -58,19 +60,8 @@ if (match) return mapUniqMatches(s, re, mapper, re.exec(s), results.concat([match])) | ||
const getPngUrl = encodedData => `${PUML_PNG_SERVER}/${encodedData}}` | ||
const getSvgUrl = encodedData => `${PUML_SVG_SERVER}/${encodedData}}` | ||
const getPumlTinyUrl = encodedData => tiny.shorten(getSvgUrl(encodedData)) | ||
const getImgUrl = (imageFormat, encodedData) => { | ||
const imgFormatToGetUrlFn = { | ||
png: getPngUrl, | ||
svg: getSvgUrl | ||
} | ||
return imgFormatToGetUrlFn[imageFormat](encodedData) | ||
} | ||
// DATA STRUCTURES | ||
class PumlLinks extends Map { | ||
constructor(pumlPaths, turnOffLinkShortening) { | ||
constructor(pumlPaths, shouldShortenLinks) { | ||
super(); | ||
this.turnOffLinkShortening = turnOffLinkShortening | ||
this.shouldShortenLinks = shouldShortenLinks | ||
// Mark paths so we know which should be visited | ||
@@ -89,3 +80,3 @@ // Any path that's not marked 0 we know isn't a puml path because it doesn't correspond to a puml fil | ||
const encodedData = plantUmlEncoder.encode(v) | ||
const url = this.turnOffLinkShortening ? getSvgUrl(encodedData) : await getPumlTinyUrl(encodedData) | ||
const url = await getPumlUrl('svg', encodedData, this.shouldShortenLinks) | ||
return super.set(pumlPath, {encodedData, url, data: v}); | ||
@@ -106,9 +97,9 @@ } | ||
const saveDiagram = async ({rootDirectory, distDirectory, pumlPath, imageFormat, encodedData}) => { | ||
const saveDiagram = async ({rootDirectory, distDirectory, pumlPath, imgFormat, encodedData}) => { | ||
const outputPath = path.join( | ||
distDirectory, pumlPath.replace(rootDirectory, '').replace(/\.puml$/, `.${imageFormat}`) | ||
distDirectory, pumlPath.replace(rootDirectory, '').replace(/\.puml$/, `.${imgFormat}`) | ||
) | ||
mkdirIfDoesntExist(path.dirname(outputPath)) | ||
const imgUrl = getImgUrl(imageFormat, encodedData) | ||
const imgUrl = getFullPumlUrl(imgFormat, encodedData) | ||
try { | ||
@@ -125,5 +116,5 @@ await downloadImg(outputPath, imgUrl) | ||
for (let [pumlPath, {encodedData}] of pumlLinks) { | ||
for (let imageFormat of imageFormats) { | ||
for (let imgFormat of imageFormats) { | ||
await saveDiagram({ | ||
distDirectory, rootDirectory, pumlPath, imageFormat, encodedData | ||
distDirectory, rootDirectory, pumlPath, imgFormat, encodedData | ||
}) | ||
@@ -160,2 +151,3 @@ } | ||
await Promise.all(mapUniqMatches(data, /\$link=["']([^"']+)['"]/gm, async ([pumlLink, pumlLinkPath]) => { | ||
// console.debug(pumlLink, pumlLinkPath) | ||
const newLink = await processPumlFile(path.resolve(path.dirname(pumlPath), pumlLinkPath), pumlLinks) | ||
@@ -178,3 +170,3 @@ data = data.replaceAll(pumlLink, `$link="${newLink.url}"`) | ||
const findMdPumlLinksRE = new RegExp( | ||
`(\\[?!?\\[?\\[[^\\[]*)?<!\-\-(!?\\[[^\\]]+\\])\\(([^)]+\\.puml)\\)\-\->`, 'g' | ||
`(\\[.*\]\\([^)]+\\))?<!\-\-(!?\\[[^\\]]+\\])\\(([^)]+\\.puml)\\)\-\->`, 'g' | ||
) | ||
@@ -189,8 +181,13 @@ // Add puml server tinyurl link for puml links indicated in markdown comments | ||
} | ||
let replacement | ||
// If the puml link is a markdown image | ||
if (linkText[0] === '!') { | ||
return `[${linkText}(${pumlLink.url})](${pumlLink.url})<!--${linkText}(${mdPumlLinkPath})-->` | ||
replacement = `[${linkText}(${pumlLink.url})](${pumlLink.url})<!--${linkText}(${mdPumlLinkPath})-->` | ||
} else { | ||
// If the puml link is a markdown hyperlink | ||
replacement = `${linkText}(${pumlLink.url})<!--${linkText}(${mdPumlLinkPath})-->` | ||
} | ||
// If the puml link is a markdown hyperlink | ||
return `${linkText}(${pumlLink.url})<!--${linkText}(${mdPumlLinkPath})-->` | ||
// console.debug({match, linkText, mdPumlLinkPath, replacement}) | ||
return replacement | ||
}) | ||
@@ -213,3 +210,3 @@ }) | ||
gitignorePath, | ||
turnOffLinkShortening, | ||
shouldShortenLinks, | ||
} | ||
@@ -223,3 +220,3 @@ ) => { | ||
const pumlPaths = glob.sync(`${pumlDirectory}/**/*.puml`, {ignore, nodir: true}) | ||
const pumlLinks = new PumlLinks(pumlPaths, turnOffLinkShortening) | ||
const pumlLinks = new PumlLinks(pumlPaths, shouldShortenLinks) | ||
@@ -230,3 +227,5 @@ for (let p of pumlPaths) await processPumlFile(p, pumlLinks) | ||
if (outputImages) { | ||
await saveDiagrams({ rootDirectory, distDirectory, imageFormats, pumlLinks }) | ||
await saveDiagrams({ | ||
rootDirectory, distDirectory, imageFormats, pumlLinks | ||
}) | ||
} | ||
@@ -233,0 +232,0 @@ } |
{ | ||
"name": "puml-for-markdown", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "An application to add interactive PUML diagrams to your github markdown files. If running with default arguments, run in project root directory.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -109,1 +109,12 @@ # puml-for-markdown | ||
though. | ||
### Other Helpful Links | ||
* [C4-Puml](https://github.com/plantuml-stdlib/C4-PlantUML): A collection of PlantUML diagrams for use in C4, you'll see me using this in my examples | ||
### More Examples | ||
[![ERD](https://tinyurl.com/ya6qvr7r)](https://tinyurl.com/ya6qvr7r)<!--![ERD](./puml/level_4_erd.puml)--> | ||
[![Container View](https://tinyurl.com/yblre3m4)](https://tinyurl.com/yblre3m4)<!--![Container View](./puml/level_2_container_view.puml)--> | ||
[![Component View - Label Retrieval Job](https://tinyurl.com/y8egw3wt)](https://tinyurl.com/y8egw3wt)<!--![Component View - Label Retrieval Job](./puml/level_3_component_view_label_retrieval_job.puml)--> | ||
[![Component View - Pipeline Component](https://tinyurl.com/y9j7twkz)](https://tinyurl.com/y9j7twkz)<!--![Component View - Pipeline Component](./puml/level_3_component_view_pipeline.puml)--> | ||
[![Activity Diagram - Sampler A](https://tinyurl.com/ybp8ju9x)](https://tinyurl.com/ybp8ju9x)<!--![Activity Diagram - Sampler A](./puml/level_4_activity_diagram_sampler_a.puml)--> | ||
[![Activity Diagram - Sampler B](https://tinyurl.com/ya3cqxkv)](https://tinyurl.com/ya3cqxkv)<!--![Activity Diagram - Sampler B](./puml/level_4_activity_diagram_sampler_b.puml)--> |
39217
120
264