PlantUML.js 891 B

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