Skip to content

Commit

Permalink
feat(mdRender): support strikethrough and autolink
Browse files Browse the repository at this point in the history
  • Loading branch information
isHarryh committed Feb 17, 2025
1 parent 81a5470 commit 32a70f6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
package cn.harryh.arkpets.utils.markdown;

import org.commonmark.ext.gfm.strikethrough.Strikethrough;
import org.commonmark.ext.gfm.tables.*;
import org.commonmark.node.*;
import org.commonmark.renderer.NodeRenderer;
Expand Down Expand Up @@ -43,15 +44,17 @@ public Set<Class<? extends Node>> getNodeTypes() {
Link.class, ListItem.class, OrderedList.class,
Image.class, Emphasis.class, StrongEmphasis.class,
Text.class, Code.class, HtmlInline.class,
SoftLineBreak.class, HardLineBreak.class, TableBlock.class,
TableHead.class, TableBody.class, TableRow.class,
TableCell.class
SoftLineBreak.class, HardLineBreak.class, Strikethrough.class,
TableBlock.class, TableHead.class, TableBody.class,
TableRow.class, TableCell.class
);
}

@Override
public void render(Node node) {
if (node instanceof TableBlock tableBlock) {
if (node instanceof Strikethrough strikethrough) {
renderStrikethrough(strikethrough);
} else if (node instanceof TableBlock tableBlock) {
renderBlock(tableBlock);
} else if (node instanceof TableHead tableHead) {
renderHead(tableHead);
Expand Down Expand Up @@ -164,7 +167,7 @@ public void visit(IndentedCodeBlock indentedCodeBlock) {
public void visit(Link link) {
textFlow.openTextFlow();

lastHref = link.getDestination();
lastHref = context.urlSanitizer().sanitizeLinkUrl(link.getDestination());
textFlow.openText(getHyperlinkAttachedAttrs(FxmlPrefabs.TEXT.getAttrs()));
visitChildren(link);
textFlow.closeText();
Expand Down Expand Up @@ -215,7 +218,7 @@ public void visit(Image image) {
String altText = altTextVisitor.getAltText();

if (lastHref == null) {
lastHref = image.getDestination();
lastHref = context.urlSanitizer().sanitizeImageUrl(image.getDestination());
}
textFlow.openText(getHyperlinkAttachedAttrs(FxmlPrefabs.HYPERLINK.getAttrs()));
writer.text("! ");
Expand Down Expand Up @@ -271,6 +274,14 @@ public void visit(HardLineBreak hardLineBreak) {
textFlow.closeTextFlow();
}

// REGION: MISC EXTENSION

protected void renderStrikethrough(Strikethrough strikethrough) {
textFlow.openText(getHyperlinkAttachedAttrs(FxmlPrefabs.STRIKETHROUGH.getAttrs()));
visitChildren(strikethrough);
textFlow.closeText();
}

// REGION: TABLE EXTENSION

protected void renderBlock(TableBlock tableBlock) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import cn.harryh.arkpets.utils.IOUtils;
import javafx.fxml.FXMLLoader;
import org.commonmark.ext.autolink.AutolinkExtension;
import org.commonmark.ext.gfm.strikethrough.StrikethroughExtension;
import org.commonmark.ext.gfm.tables.TablesExtension;
import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
Expand All @@ -25,7 +27,11 @@ public class FxmlConvertor {
*/
public static String toFxmlString(String markdown) {
Parser parser = Parser.builder()
.extensions(List.of(TablesExtension.create()))
.extensions(List.of(
AutolinkExtension.create(),
StrikethroughExtension.create(),
TablesExtension.create()
))
.build();
Node document = parser.parse(markdown);
HtmlRenderer renderer = HtmlRenderer.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ private void handleHyperlinkClick(MouseEvent event) {
}

/** Sets the consumer that accepts the URL of the hyperlink the user clicked.
* <p>
* Remember to verify the security of the URL. The URL may be:
* <ul>
* <li>Web "http://" link</li>
* <li>Web "https://" link</li>
* <li>Email "mailto:" link</li>
* <li>Relative file link</li>
* <li>Other unknown link</li>
* </ul>
* @param consumer A string consumer.
*/
public void setHyperlinkConsumer(Consumer<String> consumer) {
Expand Down
3 changes: 3 additions & 0 deletions desktop/src/cn/harryh/arkpets/utils/markdown/FxmlPrefabs.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public enum FxmlPrefabs {
STRONG_EMPHASIS(Map.of(
"style", "-fx-font-weight:bold;-fx-font-style:normal;"
)),
STRIKETHROUGH(Map.of(
"style", "-fx-strikethrough:true;"
)),
TEXT(Map.of(
)),
CODE_BLOCK(Map.of(
Expand Down

0 comments on commit 32a70f6

Please sign in to comment.