Skip to content

Commit

Permalink
extend table markdown according to what's used in AEM and add link ti…
Browse files Browse the repository at this point in the history
…tles
  • Loading branch information
stoerr committed Oct 17, 2023
1 parent 855b347 commit 2f690b4
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ protected boolean pageHandling(Resource resource, PrintWriter out, @Nonnull Appr
}
outputIfNotBlank(out, vm, "shortDescription", service);
outputIfNotBlank(out, vm, JCR_DESCRIPTION, service);
out.println();
}
return isPage;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void testPageHandlingWithPageResource() {
service.approximateMarkdown(component, printWriter, request, response);
String expectedOutput =
"# myPage\n\n" +
"The best page!\n";
"The best page!\n\n";
assertThat(writer.toString(), is(expectedOutput));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@
<text
jcr:primaryType="nt:unstructured"
sling:resourceType="wknd/components/text"
text="&lt;p>Text before&lt;/p>&#xd;&#xa;"
text="&lt;p style=&quot;text-align: left;&quot;>Normal&amp;nbsp;&lt;b>bold&lt;/b>&amp;nbsp;&lt;i>italic&lt;/i>&amp;nbsp;&lt;u>underlined&lt;/u>&amp;nbsp;text&lt;/p>&#xd;&#xa;&lt;table cellpadding=&quot;1&quot; cellspacing=&quot;0&quot; border=&quot;1&quot; width=&quot;128&quot; height=&quot;36&quot;>&#xd;&#xa;&lt;caption>The caption&lt;/caption>&#xd;&#xa;&lt;tbody>&lt;tr>&lt;th scope=&quot;col&quot;>head1&lt;/th>&#xd;&#xa;&lt;th scope=&quot;col&quot;>head2&lt;/th>&#xd;&#xa;&lt;th scope=&quot;col&quot;>head3&lt;/th>&#xd;&#xa;&lt;/tr>&lt;tr>&lt;th scope=&quot;row&quot;>row1&lt;/th>&#xd;&#xa;&lt;td>row2&lt;/td>&#xd;&#xa;&lt;td>row3&lt;/td>&#xd;&#xa;&lt;/tr>&lt;/tbody>&lt;/table>&#xd;&#xa;&lt;p style=&quot;text-align: left;&quot;>Left aligned&lt;/p>&#xd;&#xa;&lt;p style=&quot;text-align: center;&quot;>centered&lt;/p>&#xd;&#xa;&lt;p style=&quot;text-align: right;&quot;>right aligned&lt;/p>&#xd;&#xa;&lt;p style=&quot;text-align: justify;&quot;>This is justified.&amp;nbsp;&lt;a title=&quot;alternative text&quot; href=&quot;https://www.example.net&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;>This is justified.&lt;/a>&amp;nbsp;This is justified.&amp;nbsp;&lt;a href=&quot;/content/wknd/us/en/magazine.html&quot;>This is justified.&lt;/a>&amp;nbsp;This is justified.&amp;nbsp;This is justified.&amp;nbsp;This is justified.&amp;nbsp;This is justified.&amp;nbsp;This is justified. &amp;nbsp;&lt;/p>&#xd;&#xa;&lt;h1>Heading 1&lt;/h1>&#xd;&#xa;&lt;h6>Heading 6&lt;/h6>&#xd;&#xa;&lt;blockquote>a quoted quote quoting quotations let us see whether that is a block quote and what happens if it has several lines because that's interesting&lt;/blockquote>&#xd;&#xa;&lt;hr>&#xd;&#xa;&#xd;&#xa;&lt;p>&amp;nbsp;&lt;/p>&#xd;&#xa;&lt;pre>&#xd;&#xa;Preformatted text&#xd;&#xa;&lt;/pre>&#xd;&#xa;&lt;p>&amp;nbsp;&lt;/p>&#xd;&#xa;&lt;p style=&quot;text-align: center;&quot;>&amp;nbsp;&lt;/p>&#xd;&#xa;"
textIsRich="true"/>
<title
jcr:primaryType="nt:unstructured"
sling:resourceType="wknd/components/title"
jcr:title="Composum AI Testpage"/>
<testdialog
jcr:primaryType="nt:unstructured"
sling:resourceType="composum-ai/test/components/testdialog"
text="Thisisatext"
textIsRich="true"
therichText="&lt;p>Give a little bit of &lt;b>rich&lt;/b> &lt;i>text&lt;/i>&lt;/p>&lt;ul>&lt;li>he&lt;/li>&lt;li>re&lt;/li>&lt;/ul>"
thetext="This is, not surprising, some plain text which we have here.&#xd;&#xa;Another line of plain text."
Expand All @@ -56,7 +61,8 @@
<experiencefragment
jcr:primaryType="nt:unstructured"
sling:resourceType="wknd/components/experiencefragment"
fragmentVariationPath="/content/experience-fragments/wknd/us/en/adventures/adventures-2021/master"/>
fragmentVariationPath="/content/experience-fragments/wknd/us/en/adventures/adventures-2021/master"
text="New Adventures for 2021 Go somewhere incredible next year. This past year was challenging on a number of levels but we've got your back. We've made several changes and improvements to all the adventures to make them safer, more flexible and as stress-free as possible. All adventures offer a no-hassle cancellation, fully refundable, no questions asked. New! Bali Surf Camp Surfing in Bali is on the bucket list of every surfer - whether you're a beginner or someone who's been surfing for decades, there will be a break to cater to your ability. Bali offers warm water, tropical vibes, awesome breaks and low cost expenses. Bali Surf Camp"/>
</container>
</root>
</jcr:content>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import javax.annotation.Nullable;

import org.jsoup.Jsoup;
import org.jsoup.internal.StringUtil;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
Expand Down Expand Up @@ -38,7 +39,8 @@ public class HtmlToMarkdownConverter {
/**
* Important table attributes we need to keep.
*/
private static final List<String> TABLE_ATTRIBUTES = List.of("border", "colspan", "rowspan", "align", "valign", "scope");
private static final List<String> TABLE_ATTRIBUTES = List.of("border", "colspan", "rowspan",
"align", "valign", "scope", "cellpadding", "cellspacing", "width", "height", "bgcolor");

// continued indentation. Two spaces since four would be code block
private final String indentStep = " ";
Expand All @@ -50,6 +52,8 @@ public class HtmlToMarkdownConverter {

@Nonnull
public String convert(@Nullable String html) {
sb.setLength(0);
continuedIndentation = "";
if (html != null) {
Document doc = Jsoup.parseBodyFragment(html);
convertElement(doc.body());
Expand Down Expand Up @@ -102,6 +106,15 @@ private void convertElement(Element element) {
convertChildren(element);
sb.append("](");
sb.append(element.attr("href"));
String title = element.attr("title");
if (StringUtil.isBlank(title)) {
title = element.attr("alt");
}
if (!StringUtil.isBlank(title)) {
sb.append(" \"");
sb.append(title.replaceAll("\"", "\\\""));
sb.append("\"");
}
sb.append(")");
break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ public class HtmlToMarkdownConverterTest {

@Test
public void testConvertTagA() {
String html = "<a href=\"http://example.com\">click here</a>";
String markdown = converter.convert(html);
ec.checkThat(markdown, is("[click here](http://example.com)"));
ec.checkThat(converter.convert("<a href=\"http://example.com\">click here</a>"),
is("[click here](http://example.com)"));

// alt text
ec.checkThat(converter.convert("<a href=\"http://example.com\" alt=\"An example link\">click here</a>"),
is("[click here](http://example.com \"An example link\")"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ protected boolean pageHandling(Resource resource, PrintWriter out, @Nonnull Appr
if (StringUtils.isNotBlank(description)) {
out.println(helper.getMarkdown(description));
}
out.println();
}
return isPage;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void testPageHandlingWithPageResource() {

service.approximateMarkdown(component, printWriter, request, response);
String expectedOutput = "# myPage\n\n" +
"The best page!\n";
"The best page!\n\n";
assertEquals(expectedOutput, writer.toString());
}

Expand Down

0 comments on commit 2f690b4

Please sign in to comment.