Skip to content

Commit f570a2d

Browse files
kopporcalixtusAmandaGAndersonpaisleylewislucabolzonello
authored
Add HTML2MD conversion to abstract and comment fields (#10896)
* Add HTML2MD conversion to abstract and comment fields Co-authored-by: Carl Christian Snethlage <[email protected]> Co-authored-by: Amanda Anderson <[email protected]> Co-authored-by: Paisley Lewis <[email protected]> Co-authored-by: Luca Bolzonello <[email protected]> Co-authored-by: Ashwin2397 <[email protected]> * Add ClipBoardManager bridge --------- Co-authored-by: Carl Christian Snethlage <[email protected]> Co-authored-by: Amanda Anderson <[email protected]> Co-authored-by: Paisley Lewis <[email protected]> Co-authored-by: Luca Bolzonello <[email protected]> Co-authored-by: Ashwin2397 <[email protected]> Co-authored-by: Christoph <[email protected]>
1 parent 9e5e5a1 commit f570a2d

File tree

11 files changed

+74
-4
lines changed

11 files changed

+74
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
1818
- We added a new group icon column to the main table showing the icons of the entry's groups. [#10801](https://github.com/JabRef/jabref/pull/10801)
1919
- We added ability to jump to an entry in the command line using `-j CITATIONKEY`. [koppor#540](https://github.com/koppor/jabref/issues/540)
2020
- We added a new boolean to the style files for Openoffice/Libreoffice integration to switch between ZERO_WIDTH_SPACE (default) and no space. [#10843](https://github.com/JabRef/jabref/pull/10843)
21+
- When pasting HTML into the abstract or a comment field, the hypertext is automatically converted to Markdown. [#10558](https://github.com/JabRef/jabref/issues/10558)
2122
- We added the possibility to redownload files that had been present but are no longer in the specified location. [#10848](https://github.com/JabRef/jabref/issues/10848)
2223
- We added the citation key pattern `[camelN]`. Equivalent to the first N words of the `[camel]` pattern.
2324

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ dependencies {
215215
}
216216

217217
implementation 'com.vladsch.flexmark:flexmark:0.64.8'
218+
implementation 'com.vladsch.flexmark:flexmark-html2md-converter:0.64.8'
218219

219220
implementation group: 'net.harawata', name: 'appdirs', version: '1.2.2'
220221

external-libraries.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,13 @@ URL: https://github.com/vsch/flexmark-java
232232
License: BSD-2-Clause
233233
```
234234
235+
```yaml
236+
Id: com.vladsch.flexmark:flexmark-html2md-converter
237+
Project: flexmark-java
238+
URL: https://github.com/vsch/flexmark-java
239+
License: BSD-2-Clause
240+
```
241+
235242
```yaml
236243
Id: commons-beanutils:commons-beanutils
237244
Project: Apache Commons Beanutils
@@ -708,6 +715,7 @@ com.squareup.okio:okio:1.15.0
708715
com.squareup.retrofit2:retrofit:2.6.1
709716
com.sun.istack:istack-commons-runtime:4.1.2
710717
com.tobiasdiez:easybind:2.2.1-SNAPSHOT
718+
com.vladsch.flexmark:flexmark-html2md-converter:0.64.8
711719
com.vladsch.flexmark:flexmark-util-ast:0.64.8
712720
com.vladsch.flexmark:flexmark-util-builder:0.64.8
713721
com.vladsch.flexmark:flexmark-util-collection:0.64.8

src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
requires com.ibm.icu;
109109

110110
requires flexmark;
111+
requires flexmark.html2md.converter;
111112
requires flexmark.util.ast;
112113
requires flexmark.util.data;
113114

src/main/java/org/jabref/gui/ClipBoardManager.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ public static String getContents() {
8787
return result;
8888
}
8989

90+
public static String getHtmlContents() {
91+
String result = clipboard.getHtml();
92+
if (result == null) {
93+
return "";
94+
}
95+
return result;
96+
}
97+
98+
public static boolean hasHtml() {
99+
return clipboard.hasHtml();
100+
}
101+
90102
/**
91103
* Get the String residing on the primary clipboard (if it exists).
92104
*

src/main/java/org/jabref/gui/fieldeditors/FieldEditors.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ public static FieldEditorFX getForField(final Field field,
9999
return new KeywordsEditor(field, suggestionProvider, fieldCheckers, preferences, undoManager);
100100
} else if (field == InternalField.KEY_FIELD) {
101101
return new CitationKeyEditor(field, suggestionProvider, fieldCheckers, databaseContext);
102+
} else if (fieldProperties.contains(FieldProperty.MARKDOWN)) {
103+
return new MarkdownEditor(field, suggestionProvider, fieldCheckers, preferences, undoManager);
102104
} else {
103105
// default
104106
return new SimpleEditor(field, suggestionProvider, fieldCheckers, preferences, isMultiLine, undoManager);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.jabref.gui.fieldeditors;
2+
3+
import javax.swing.undo.UndoManager;
4+
5+
import javafx.scene.control.TextInputControl;
6+
7+
import org.jabref.gui.ClipBoardManager;
8+
import org.jabref.gui.autocompleter.SuggestionProvider;
9+
import org.jabref.logic.integrity.FieldCheckers;
10+
import org.jabref.model.entry.field.Field;
11+
import org.jabref.preferences.PreferencesService;
12+
13+
import com.vladsch.flexmark.html2md.converter.FlexmarkHtmlConverter;
14+
15+
public class MarkdownEditor extends SimpleEditor {
16+
17+
private final FlexmarkHtmlConverter flexmarkHtmlConverter = FlexmarkHtmlConverter.builder().build();
18+
19+
public MarkdownEditor(Field field, SuggestionProvider<?> suggestionProvider, FieldCheckers fieldCheckers, PreferencesService preferences, UndoManager undoManager) {
20+
super(field, suggestionProvider, fieldCheckers, preferences, true, undoManager);
21+
}
22+
23+
@Override
24+
protected TextInputControl createTextInputControl() {
25+
return new EditorTextArea() {
26+
@Override
27+
public void paste() {
28+
if (ClipBoardManager.hasHtml()) {
29+
String htmlText = ClipBoardManager.getHtmlContents();
30+
String mdText = flexmarkHtmlConverter.convert(htmlText);
31+
super.replaceSelection(mdText);
32+
} else {
33+
super.paste();
34+
}
35+
}
36+
};
37+
}
38+
}

src/main/java/org/jabref/gui/fieldeditors/SimpleEditor.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class SimpleEditor extends HBox implements FieldEditorFX {
2020

2121
private final SimpleEditorViewModel viewModel;
2222
private final TextInputControl textInput;
23+
private final boolean isMultiLine;
2324

2425
public SimpleEditor(final Field field,
2526
final SuggestionProvider<?> suggestionProvider,
@@ -28,8 +29,9 @@ public SimpleEditor(final Field field,
2829
final boolean isMultiLine,
2930
final UndoManager undoManager) {
3031
this.viewModel = new SimpleEditorViewModel(field, suggestionProvider, fieldCheckers, undoManager);
32+
this.isMultiLine = isMultiLine;
3133

32-
textInput = isMultiLine ? new EditorTextArea() : new EditorTextField();
34+
textInput = createTextInputControl();
3335
HBox.setHgrow(textInput, Priority.ALWAYS);
3436

3537
textInput.textProperty().bindBidirectional(viewModel.textProperty());
@@ -55,6 +57,10 @@ public SimpleEditor(final Field field,
5557
this(field, suggestionProvider, fieldCheckers, preferences, false, undoManager);
5658
}
5759

60+
protected TextInputControl createTextInputControl() {
61+
return isMultiLine ? new EditorTextArea() : new EditorTextField();
62+
}
63+
5864
@Override
5965
public void bindToEntry(BibEntry entry) {
6066
viewModel.bindToEntry(entry);

src/main/java/org/jabref/model/entry/field/FieldProperty.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public enum FieldProperty {
1313
ISSN,
1414
JOURNAL_NAME,
1515
LANGUAGE,
16+
MARKDOWN, // Field content is text, but should be interpreted as markdown
1617
MONTH,
1718
MULTIPLE_ENTRY_LINK,
1819
MULTILINE_TEXT,

src/main/java/org/jabref/model/entry/field/StandardField.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515
public enum StandardField implements Field {
1616

17-
ABSTRACT("abstract", FieldProperty.MULTILINE_TEXT),
17+
ABSTRACT("abstract", FieldProperty.MULTILINE_TEXT, FieldProperty.MARKDOWN),
1818
ADDENDUM("addendum"),
1919
ADDRESS("address"),
2020
AFTERWORD("afterword", FieldProperty.PERSON_NAMES),
@@ -32,7 +32,7 @@ public enum StandardField implements Field {
3232
CHAPTER("chapter"),
3333
COMMENTATOR("commentator", FieldProperty.PERSON_NAMES),
3434
// Comments of users are handled at {@link org.jabref.model.entry.field.UserSpecificCommentField}
35-
COMMENT("comment", FieldProperty.COMMENT, FieldProperty.MULTILINE_TEXT, FieldProperty.VERBATIM),
35+
COMMENT("comment", FieldProperty.COMMENT, FieldProperty.MULTILINE_TEXT, FieldProperty.VERBATIM, FieldProperty.MARKDOWN),
3636
CROSSREF("crossref", FieldProperty.SINGLE_ENTRY_LINK),
3737
CITES("cites", FieldProperty.MULTIPLE_ENTRY_LINK),
3838
DATE("date", FieldProperty.DATE),

0 commit comments

Comments
 (0)