Update text logic and change workaround for markdown

This commit is contained in:
yari-dewalt
2024-09-18 12:54:32 -07:00
parent 745495c2d5
commit 25337dc453
3 changed files with 12 additions and 38 deletions

View File

@@ -123,42 +123,13 @@ export class ClassMember {
}
this.classifier = potentialClassifier;
// TODO: Right now getting rid of spacing in id / name, TODO: Add optional spacing
const combinedText = `${this.visibility}${this.id}${this.memberType === 'method' ? `(${this.parameters})${this.returnType ? ' : ' + this.returnType : ''}` : ''}`;
this.text = combinedText;
if (combinedText.includes('~')) {
const numOfTildes = (combinedText.substring(1).match(/~/g) ?? []).length;
let count = numOfTildes;
if (count !== 1) {
const odd = count % 2 > 0;
// Preserve one space only
this.id = this.id.startsWith(' ') ? ' ' + this.id.trim() : this.id.trim();
// Replace all '~' with '>'
let replacedRaw = combinedText.substring(1).replaceAll('~', '>');
// Replace the first half of '>' with '<'
while (count > 0) {
replacedRaw = replacedRaw.replace('&gt;', '&lt;');
count -= 2; // Each iteration replaces one '>' with '<', so reduce count by 2
}
if (odd) {
// Replace first occurrence.
if (this.memberType === 'method') {
replacedRaw = replacedRaw.replace('&lt;', '~');
} else {
// Replace middle occurrence.
const ltOccurrences = replacedRaw.match(/&lt;/g) ?? [];
if (ltOccurrences.length > 1) {
let ltCount = 0;
replacedRaw = replacedRaw.replace('&lt;', (match) => {
ltCount++;
return ltCount === ltOccurrences.length ? '~' : match;
});
}
}
}
this.text = this.text.charAt(0) + replacedRaw;
}
const combinedText = `${this.visibility ? '\\' + this.visibility : ''}${parseGenericTypes(this.id)}${this.memberType === 'method' ? `(${parseGenericTypes(this.parameters)})${this.returnType ? ' : ' + parseGenericTypes(this.returnType) : ''}` : ''}`;
this.text = combinedText.replaceAll('<', '&lt;').replaceAll('>', '&gt;');
if (this.text.startsWith('\\&lt;')) {
this.text = this.text.replace('\\&lt;', '~');
}
}

View File

@@ -109,6 +109,12 @@ const addText = async (
textContent = node.label!;
}
// createText() will cause unwanted behavior because of classDiagram syntax so workarounds are needed
if (!useHtmlLabels && textContent.startsWith('\\')) {
textContent = textContent.substring(1);
}
if (hasKatex(textContent)) {
useHtmlLabels = true;
}
@@ -126,7 +132,6 @@ const addText = async (
let bbox;
let numberOfLines = 1;
// createText() creates unwanted behavior because of syntax, so fix
if (!useHtmlLabels) {
// Undo font-weight
select(text).selectAll('tspan').attr('font-weight', '');

View File

@@ -87,8 +87,6 @@ export function markdownToHTML(markdown: string, { markdownAutoWrap }: MermaidCo
return `${node.text}`;
} else if (node.type === 'escape') {
return node.text;
} else if (node.type === 'list') {
return node.raw;
}
return `Unsupported markdown: ${node.type}`;
}