Skip to content
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

Use a string function rather than regex for better performance #1

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions flexmark-html2md-converter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<version>0.64.8</version>
</parent>

<groupId>com.diffbot</groupId>
<artifactId>flexmark-html2md-converter</artifactId>
<name>flexmark-java HTML to Markdown extensible converter</name>
<description>flexmark-java customizable extension to convert HTML to Markdown</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1199,7 +1199,7 @@ private void handleTableCaption(Element element, HtmlNodeConverterContext contex
}

private void handleTableCell(Element element, HtmlNodeConverterContext context, HtmlMarkdownWriter out) {
String cellText = context.processTextNodes(element).trim().replaceAll("\\s*\n\\s*", " ");
String cellText = replaceMultipleBlankSpace(context.processTextNodes(element).trim());
int colSpan = 1;
int rowSpan = 1;
CellAlignment alignment = null;
Expand Down Expand Up @@ -1260,6 +1260,25 @@ private void handleTableCell(Element element, HtmlNodeConverterContext context,
}
}

private String replaceMultipleBlankSpace(String cellText) {
StringBuilder result = new StringBuilder();
boolean wasSpace = false;

for (char c : cellText.toCharArray()) {
if (Character.isWhitespace(c)) {
if (!wasSpace) {
result.append(' ');
wasSpace = true;
}
} else {
result.append(c);
wasSpace = false;
}
}

return result.toString();
}

private boolean matchingText(Pattern pattern, String text, String[] match) {
Matcher matcher = pattern.matcher(text);
if (matcher.matches()) {
Expand Down
6 changes: 2 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,10 @@

<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<id>diffbot-nexus-snapshots</id>
</snapshotRepository>
<repository>
<id>ossrh</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
<id>diffbot-nexus-releases</id>
</repository>
</distributionManagement>

Expand Down