PlantUML.js 930 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import plantuml from 'plantuml-encoder';
  2. import crypto from 'crypto';
  3. import * as entities from 'entities';
  4. export default class PlantUML {
  5. constructor(crowi) {
  6. this.crowi = crowi;
  7. }
  8. generateId(token) {
  9. const hasher = require('crypto').createHash('md5');
  10. hasher.update(token);
  11. return hasher.digest('hex');
  12. }
  13. process(code, lang) {
  14. const config = crowi.getConfig();
  15. if (!config.env.PLANTUML_URI) {
  16. return `<pre class="wiki-code"><code>${entities.encodeHTML(code)}\n</code></pre>`;
  17. }
  18. let plantumlUri = config.env.PLANTUML_URI;
  19. if (plantumlUri.substr(-1) !== '/') {
  20. plantumlUri += '/';
  21. }
  22. const id = this.generateId(code + lang);
  23. const encoded = plantuml.encode(`@startuml
  24. skinparam monochrome true
  25. ${code}
  26. @enduml`);
  27. return `
  28. <div id="${id}" class="plantuml noborder">
  29. <img src="${plantumlUri}svg/${encoded}">
  30. </div>
  31. `;
  32. }
  33. }