-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Add Copy markdown to copy citation #13387
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
base: main
Are you sure you want to change the base?
Changes from 20 commits
b9ab287
9de6531
ad09503
84a1dd1
8bed13b
fae3c18
49b035b
8654adc
d582566
9c32813
508b93d
d131b92
0553179
529c9f0
e805e7e
7b3fbf7
b3eb93c
b1811e2
34b387f
756efcf
f6b4131
85dce83
76eae89
9071097
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
|
||
import com.airhacks.afterburner.injection.Injector; | ||
import com.google.common.annotations.VisibleForTesting; | ||
import com.vladsch.flexmark.html2md.converter.FlexmarkHtmlConverter; | ||
|
||
public class ClipboardContentGenerator { | ||
|
||
|
@@ -45,6 +46,7 @@ public ClipboardContent generate(List<BibEntry> selectedEntries, CitationStyleOu | |
return switch (outputFormat) { | ||
case HTML -> processHtml(citations); | ||
case TEXT -> processText(citations); | ||
case MARKDOWN -> processMarkdown(citations); | ||
}; | ||
} else { | ||
// if it is not a citation style take care of the preview | ||
|
@@ -120,6 +122,32 @@ static ClipboardContent processHtml(List<String> citations) { | |
return content; | ||
} | ||
|
||
/** | ||
* Insert each citation into HTML. | ||
* convert HTML to markdown using flexmark. | ||
*/ | ||
@VisibleForTesting | ||
static ClipboardContent processMarkdown(List<String> citations) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method uses string concatenation for multiline HTML template instead of Java text blocks ("""). This makes the code less readable and maintainable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You followed @Siedlerchr's recommendation and did not follow mine to use citeproc-java conversion. (michel-kraemer/citeproc-java@66cad6b) There needs to be AT LEAST a comment, why citeproc-java could not be used. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was unaware of this, good point There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe my comment here #13387 (comment) is of help? Just to try out how to route things to that plugin? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because it works for all previews! Not just csl. I see no advantage in using citeproc here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My only point is that find citeproc more configurable than flexmark. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would do case handling If CSL use citeproc (specialized!, e.g. DOI linked) If no CSL, use flexmark |
||
String result = "<!DOCTYPE html>" + OS.NEWLINE + | ||
"<html>" + OS.NEWLINE + | ||
" <head>" + OS.NEWLINE + | ||
" <meta charset=\"utf-8\">" + OS.NEWLINE + | ||
" </head>" + OS.NEWLINE + | ||
" <body>" + OS.NEWLINE + OS.NEWLINE; | ||
|
||
result += String.join(CitationStyleOutputFormat.HTML.getLineSeparator(), citations); | ||
result += OS.NEWLINE + | ||
" </body>" + OS.NEWLINE + | ||
"</html>" + OS.NEWLINE; | ||
|
||
FlexmarkHtmlConverter converter = FlexmarkHtmlConverter.builder().build(); | ||
String markdown = converter.convert(result); | ||
|
||
ClipboardContent content = new ClipboardContent(); | ||
content.putString(markdown); | ||
return content; | ||
} | ||
|
||
private List<String> generateTextBasedPreviewLayoutCitations(List<BibEntry> selectedEntries, BibDatabaseContext bibDatabaseContext) throws IOException { | ||
TextBasedPreviewLayout customPreviewLayout = previewPreferences.getCustomPreviewLayout(); | ||
Reader customLayoutReader = Reader.of(customPreviewLayout.getText().replace("__NEWLINE__", "\n")); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -138,4 +138,32 @@ void processHtmlAsHtml() { | |
Object actual = htmlTransferable.getHtml(); | ||
assertEquals(expected, actual); | ||
} | ||
|
||
@Test | ||
void processMarkdownAsMarkdown() { | ||
String expected = "\\[1\\] \n" + | ||
"B. Smith, B. Jones, and J. Williams, \"Title of the test entry,\" *BibTeX Journal*, vol. 34, no. 3, pp. 45--67, Jul. 2016.\n" + | ||
"\n" + | ||
"\\[1\\] \n" + | ||
"B. Smith, B. Jones, and J. Williams, \"Title of the test entry,\" *BibTeX Journal*, vol. 34, no. 3, pp. 45--67, Jul. 2016.\n"; | ||
|
||
String citation = " <div class=\"csl-entry\">" + OS.NEWLINE + | ||
" <div class=\"csl-left-margin\">[1]</div><div class=\"csl-right-inline\">B. Smith, B. Jones, and J. Williams, “Title of the test entry,” <i>BibTeX Journal</i>, vol. 34, no. 3, pp. 45–67, Jul. 2016.</div>" + OS.NEWLINE + | ||
" </div>" + OS.NEWLINE; | ||
|
||
ClipboardContent markdown = ClipboardContentGenerator.processMarkdown(Arrays.asList(citation, citation)); | ||
String actual = markdown.getString(); | ||
|
||
// Print actual output for debugging | ||
System.out.println("=== Actual Markdown Output ==="); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for these, on failure assertEquals will show the diff |
||
System.out.println(actual); | ||
System.out.println("=== End ==="); | ||
|
||
// Normalize both strings to ignore whitespace and formatting issues | ||
String normalizedActual = actual.replaceAll("\\s+", " ").trim(); | ||
String normalizedExpected = expected.replaceAll("\\s+", " ").trim(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What are "formatting issues" here specifically? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually you are right I think I was getting cautious that is why I was removing whitespaces. |
||
|
||
assertEquals(normalizedExpected, normalizedActual); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment is trivial and simply restates what the code does without providing additional information about the reasoning or important details about the implementation.