-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch out commonmark for prism rendering of text
- Loading branch information
Showing
5 changed files
with
33 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,39 @@ | ||
package org.cftoolsuite.ui.component; | ||
|
||
import org.commonmark.node.Node; | ||
import org.commonmark.parser.Parser; | ||
import org.commonmark.renderer.html.HtmlRenderer; | ||
import com.vaadin.flow.component.Html; | ||
import com.vaadin.flow.component.Tag; | ||
import com.vaadin.flow.component.dependency.JavaScript; | ||
import com.vaadin.flow.component.dependency.StyleSheet; | ||
|
||
import com.vaadin.flow.component.Composite; | ||
import com.vaadin.flow.component.html.Div; | ||
import com.vaadin.flow.component.html.Span; | ||
@Tag("div") | ||
@JavaScript("https://cdnjs.cloudflare.com/ajax/libs/prism/9000.0.1/prism.min.js") | ||
@JavaScript("https://cdnjs.cloudflare.com/ajax/libs/prism/9000.0.1/components/prism-markdown.min.js") | ||
@StyleSheet("https://cdnjs.cloudflare.com/ajax/libs/prism-themes/1.9.0/prism-nord.min.css") | ||
public class Markdown extends Html { | ||
|
||
public class Markdown extends Composite<Div> { | ||
|
||
private final Parser parser = Parser.builder().build(); | ||
private final HtmlRenderer renderer = HtmlRenderer.builder().build(); | ||
private final StringBuilder markdownBuffer = new StringBuilder(); | ||
private final Span rawTextSpan = new Span(); | ||
private boolean isRendered = false; | ||
private StringBuilder builder = new StringBuilder(); | ||
|
||
public Markdown() { | ||
getContent().add(rawTextSpan); | ||
} | ||
|
||
public Markdown(String markdown) { | ||
this(); | ||
setMarkdown(markdown); | ||
} | ||
|
||
public void setMarkdown(String markdown) { | ||
markdownBuffer.setLength(0); | ||
rawTextSpan.setText(""); | ||
addMarkdown(markdown); | ||
} | ||
|
||
public void addMarkdown(String markdown) { | ||
markdownBuffer.append(markdown).append(" "); | ||
rawTextSpan.setText(markdownBuffer.toString()); | ||
super("<pre><code class=\"language-markdown\"></code></pre>"); | ||
getElement().executeJs("Prism.highlightElement($0)", getElement()); | ||
} | ||
|
||
public void render() { | ||
if (!isRendered) { | ||
Node document = parser.parse(markdownBuffer.toString()); | ||
String html = renderer.render(document); | ||
getContent().removeAll(); | ||
getContent().getElement().setProperty("innerHTML", html); | ||
isRendered = true; | ||
public void setSource(String code) { | ||
if (code.startsWith("-")) { | ||
builder.append(System.lineSeparator()); | ||
builder.append(System.lineSeparator()); | ||
} | ||
builder.append(" " + code); | ||
setHtmlContent("<pre><code class=\"language-markdown\">" + | ||
escapeHtml(builder.toString()) + "</code></pre>"); | ||
getElement().executeJs("Prism.highlightElement($0)", getElement()); | ||
} | ||
|
||
public void reset() { | ||
markdownBuffer.setLength(0); | ||
rawTextSpan.setText(""); | ||
getContent().removeAll(); | ||
getContent().add(rawTextSpan); | ||
isRendered = false; | ||
private static String escapeHtml(String html) { | ||
return html.replace("&", "&") | ||
.replace("<", "<") | ||
.replace(">", ">") | ||
.replace("\"", """) | ||
.replace("'", "'"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters