JSFiddle - React, Tailwind, and code Playground

by Abhishek Kumar

JavaScript

/**
 * Generates a flowchart from a PlantUML script using Unicode characters.
 *
 * @param {string} plantumlScript - The PlantUML script to generate the flowchart from.
 * @returns {string} The generated flowchart as a string.
 */
function generateFlowchart(plantumlScript) {
  // Define the Unicode characters for the flowchart elements
  const elements = {
    participant: '',
    actor: '',
    usecase: '',
    class: '',
    interface: '',
    package: '',
    arrow: '→',
    dashedArrow: '⇢',
    boldArrow: '⇨',
  };

  // Split the PlantUML script into lines
  const lines = plantumlScript.split('\n');

  // Initialize the flowchart string
  let flowchart = '';

  // Iterate over the lines in the PlantUML script
  for (const line of lines) {
    // Trim the line
    const trimmedLine = line.trim();

    // Check if the line starts with a keyword
    if (trimmedLine.startsWith('participant')) {
      // Extract the participant name
      const participantName = trimmedLine.substring(11).trim();
      flowchart += `${elements.participant} ${participantName}\n`;
    } else if (trimmedLine.startsWith('actor')) {
      // Extract the actor name
      const actorName = trimmedLine.substring(6).trim();
      flowchart += `${elements.actor} ${actorName}\n`;
    } else if (trimmedLine.startsWith('usecase')) {
      // Extract the use case name
      const useCaseName = trimmedLine.substring(8).trim();
      flowchart += `${elements.usecase} ${useCaseName}\n`;
    } else if (trimmedLine.startsWith('class')) {
      // Extract the class name
      const className = trimmedLine.substring(6).trim();
      flowchart += `${elements.class} ${className}\n`;
    } else if (trimmedLine.startsWith('interface')) {
      // Extract the interface name
      const interfaceName = trimmedLine.substring(10).trim();
      flowchart += `${elements.interface} ${interfaceName}\n`;
    } else if (trimmedLine.startsWith('package')) {
      // Extract the package name
      const packageName...