-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Add Markdown rendering to AI Chat #13194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Yubo-Cao
wants to merge
24
commits into
JabRef:main
Choose a base branch
from
Yubo-Cao:markdown-ai
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
e04f0de
feat: markdown in AI messages
Yubo-Cao ebcd900
docs: update CHANGELOG
Yubo-Cao ae2e2fa
Merge branch 'JabRef:main' into markdown-ai
Yubo-Cao 2b2fa66
fix: note that Markdown is always valid, it never throw.
Yubo-Cao be60b42
Merge branch 'markdown-ai' of https://github.com/Yubo-Cao/jabref into…
Yubo-Cao 3336504
chore: move Flexmark dependency up
Yubo-Cao 17becfb
fix: remove null check for scnee
Yubo-Cao abd52c0
docs: remove trivial documentation
Yubo-Cao 99a6c3d
Improve whitespace and MarkdownTextflow's coping experience
9e4ccfc
Merge remote-tracking branch 'upstream/main' into markdown-ai
9c31517
Use Deque and StringJoiner per code review.
4e2c604
Remove == null check and fix getScene()'s NPE
abdc1d3
Use @NonNull, rather than Objects.requireNonNull
22ae09c
Get rid of width binding.
d9eaa0b
Merge branch 'main' into markdown-ai
Yubo-Cao 2017cc4
Merge branch 'main' into markdown-ai
subhramit 51e63d3
Apply Subhramit's suggestions
8348525
Fix drag event's problem as ThiloteE suggests.
aed3c58
Add Link support to the Markdown rendering
8edce1c
Fix according to Trag
fda54f7
Fix according to Trag
699d3eb
Fix according to Trag
e3fa14f
Fix according to Ruslan
2eca1a3
Fix according to Trag
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
25 changes: 25 additions & 0 deletions
25
jabgui/src/main/java/org/jabref/gui/keyboard/SelectableTextFlowKeyBindings.java
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package org.jabref.gui.keyboard; | ||
|
||
import javafx.scene.Scene; | ||
import javafx.scene.input.KeyEvent; | ||
|
||
import org.jabref.gui.util.SelectableTextFlow; | ||
|
||
public class SelectableTextFlowKeyBindings { | ||
public static void call(Scene scene, KeyEvent event, KeyBindingRepository keyBindingRepository) { | ||
if (scene.focusOwnerProperty().get() instanceof SelectableTextFlow selectableTextFlow) { | ||
keyBindingRepository.mapToKeyBinding(event).ifPresent(binding -> { | ||
switch (binding) { | ||
case COPY -> { | ||
selectableTextFlow.copySelectedText(); | ||
event.consume(); | ||
} | ||
case SELECT_ALL -> { | ||
selectableTextFlow.selectAll(); | ||
event.consume(); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
} |
176 changes: 176 additions & 0 deletions
176
jabgui/src/main/java/org/jabref/gui/util/MarkdownTextFlow.java
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,176 @@ | ||
package org.jabref.gui.util; | ||
|
||
import javafx.geometry.NodeOrientation; | ||
import javafx.scene.layout.Pane; | ||
import javafx.scene.text.Text; | ||
|
||
import com.vladsch.flexmark.ast.BulletList; | ||
import com.vladsch.flexmark.ast.BulletListItem; | ||
import com.vladsch.flexmark.ast.Code; | ||
import com.vladsch.flexmark.ast.Emphasis; | ||
import com.vladsch.flexmark.ast.Heading; | ||
import com.vladsch.flexmark.ast.OrderedList; | ||
import com.vladsch.flexmark.ast.OrderedListItem; | ||
import com.vladsch.flexmark.ast.Paragraph; | ||
import com.vladsch.flexmark.ast.StrongEmphasis; | ||
import com.vladsch.flexmark.parser.Parser; | ||
import com.vladsch.flexmark.util.ast.Node; | ||
import com.vladsch.flexmark.util.ast.NodeVisitor; | ||
import com.vladsch.flexmark.util.ast.VisitHandler; | ||
import com.vladsch.flexmark.util.data.MutableDataSet; | ||
|
||
/** | ||
* A TextFlow that renders Markdown text with support for headings, paragraphs, emphasis, strong emphasis, code blocks, bullet lists, and ordered lists. | ||
*/ | ||
public class MarkdownTextFlow extends SelectableTextFlow { | ||
private final Parser parser; | ||
private boolean needsLineBreak = false; | ||
private int currentOrderedListIndex = 0; | ||
|
||
/** | ||
* Creates a MarkdownTextFlow instance. | ||
* | ||
* @param parent The parent Pane to which this TextFlow will be added. | ||
*/ | ||
public MarkdownTextFlow(Pane parent) { | ||
super(parent); | ||
this.setNodeOrientation(NodeOrientation.LEFT_TO_RIGHT); | ||
this.getStyleClass().add("markdown-textflow"); | ||
|
||
MutableDataSet options = new MutableDataSet(); | ||
this.parser = Parser.builder(options).build(); | ||
} | ||
|
||
/** | ||
* Sets the Markdown text to be rendered in this TextFlow. | ||
* | ||
* @param markdownText The Markdown text to render. If null or empty, the TextFlow will be cleared. | ||
*/ | ||
public void setMarkdown(String markdownText) { | ||
Yubo-Cao marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.clear(); | ||
needsLineBreak = false; | ||
|
||
if (markdownText == null || markdownText.trim().isEmpty()) { | ||
Yubo-Cao marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return; | ||
} | ||
|
||
Node document = parser.parse(markdownText); | ||
MarkdownRenderer renderer = new MarkdownRenderer(); | ||
renderer.visit(document); | ||
} | ||
|
||
private void addLineBreak() { | ||
if (needsLineBreak && !this.getChildren().isEmpty()) { | ||
Text lineBreak = new Text("\n"); | ||
this.getChildren().add(lineBreak); | ||
} | ||
needsLineBreak = false; | ||
} | ||
|
||
private class MarkdownRenderer { | ||
private final NodeVisitor visitor = new NodeVisitor( | ||
new VisitHandler<>(Heading.class, this::visit), | ||
new VisitHandler<>(Paragraph.class, this::visit), | ||
new VisitHandler<>(com.vladsch.flexmark.ast.Text.class, this::visit), | ||
new VisitHandler<>(Emphasis.class, this::visit), | ||
new VisitHandler<>(StrongEmphasis.class, this::visit), | ||
new VisitHandler<>(Code.class, this::visit), | ||
new VisitHandler<>(BulletList.class, this::visit), | ||
new VisitHandler<>(OrderedList.class, this::visit), | ||
new VisitHandler<>(BulletListItem.class, this::visit), | ||
new VisitHandler<>(OrderedListItem.class, this::visit) | ||
); | ||
|
||
public void visit(Node node) { | ||
visitor.visit(node); | ||
} | ||
|
||
private void visit(Heading heading) { | ||
addLineBreak(); | ||
|
||
String content = heading.getText().toString(); | ||
int level = heading.getLevel(); | ||
|
||
Text headingNode = new Text(content); | ||
headingNode.getStyleClass().add("markdown-h" + level); | ||
|
||
getChildren().add(headingNode); | ||
needsLineBreak = true; | ||
} | ||
|
||
private void visit(Paragraph paragraph) { | ||
addLineBreak(); | ||
|
||
Node parent = paragraph.getParent(); | ||
boolean isInList = parent instanceof BulletListItem || parent instanceof OrderedListItem; | ||
|
||
if (!isInList && !getChildren().isEmpty()) { | ||
addLineBreak(); | ||
} | ||
|
||
visitor.visitChildren(paragraph); | ||
needsLineBreak = true; | ||
} | ||
|
||
private void visit(com.vladsch.flexmark.ast.Text text) { | ||
String content = text.getChars().toString(); | ||
Text textNode = new Text(content); | ||
getChildren().add(textNode); | ||
} | ||
|
||
private void visit(Emphasis emphasis) { | ||
String content = emphasis.getText().toString(); | ||
Text italicText = new Text(content); | ||
italicText.getStyleClass().add("markdown-italic"); | ||
getChildren().add(italicText); | ||
} | ||
|
||
private void visit(StrongEmphasis strongEmphasis) { | ||
String content = strongEmphasis.getText().toString(); | ||
Text boldText = new Text(content); | ||
boldText.getStyleClass().add("markdown-bold"); | ||
getChildren().add(boldText); | ||
} | ||
|
||
private void visit(Code code) { | ||
String content = code.getText().toString(); | ||
Text codeText = new Text(content); | ||
codeText.getStyleClass().add("markdown-code"); | ||
getChildren().add(codeText); | ||
} | ||
|
||
private void visit(BulletList bulletList) { | ||
addLineBreak(); | ||
visitor.visitChildren(bulletList); | ||
needsLineBreak = true; | ||
} | ||
|
||
private void visit(OrderedList orderedList) { | ||
addLineBreak(); | ||
currentOrderedListIndex = 0; | ||
visitor.visitChildren(orderedList); | ||
needsLineBreak = true; | ||
} | ||
|
||
private void visit(BulletListItem listItem) { | ||
addLineBreak(); | ||
|
||
Text bulletText = new Text("• "); | ||
bulletText.getStyleClass().add("markdown-list-bullet"); | ||
getChildren().add(bulletText); | ||
|
||
visitor.visitChildren(listItem); | ||
} | ||
|
||
private void visit(OrderedListItem listItem) { | ||
addLineBreak(); | ||
currentOrderedListIndex++; | ||
|
||
Text numberText = new Text(currentOrderedListIndex + ". "); | ||
numberText.getStyleClass().add("markdown-list-number"); | ||
getChildren().add(numberText); | ||
|
||
visitor.visitChildren(listItem); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.