FHIR-40387 Improve guidance for markdown datatype

This commit is contained in:
Grahame Grieve 2023-02-16 13:03:37 +11:00
parent 3011b37df4
commit fecf024952
2 changed files with 54 additions and 0 deletions

View File

@ -114,4 +114,42 @@ public class MarkDownProcessor {
return !description.contains("\n");
}
public static String makeStringSafeAsMarkdown(String content) {
StringBuilder b = new StringBuilder();
for (char c : content.toCharArray()) {
if (isEscableMarkdownChar(c)) {
b.append('\\');
}
b.append(c);
}
return b.toString();
}
private static boolean isEscableMarkdownChar(char c) {
switch (c) {
case '*':
case '&':
case '#':
case '[':
case '>':
case '<':
case '`':
return true;
default:
return false;
}
}
public static String makeMarkdownForString(String content) {
StringBuilder b = new StringBuilder();
for (int i = 0; i < content.length(); i++) {
char c = content.charAt(i);
if (c != '\\' || i == content.length() - 1 || !isEscableMarkdownChar(content.charAt(i+1))) {
b.append(c);
}
}
return b.toString();
}
}

View File

@ -0,0 +1,16 @@
package org.hl7.fhir.utilities;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class MarkdownTests {
@Test
void testStringToMarkdown() {
Assertions.assertEquals("This is \\*a test\\*", MarkDownProcessor.makeStringSafeAsMarkdown("This is *a test*"));
Assertions.assertEquals("This is *a test*", MarkDownProcessor.makeMarkdownForString("This is \\*a test\\*"));
}
}