This is an automated email from the ASF dual-hosted git repository. kwin pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/maven-doxia.git
The following commit(s) were added to refs/heads/master by this push: new 4a199816 [DOXIA-759] Support anchors in MarkdownSink (#247) 4a199816 is described below commit 4a1998161aa2bb8f8ee48cefc592022223cf1af0 Author: Konrad Windszus <k...@apache.org> AuthorDate: Sat Nov 9 10:22:45 2024 +0100 [DOXIA-759] Support anchors in MarkdownSink (#247) --- .../maven/doxia/module/markdown/MarkdownSink.java | 27 +++++++++++++++++++--- .../doxia/module/markdown/MarkdownSinkTest.java | 2 +- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/doxia-modules/doxia-module-markdown/src/main/java/org/apache/maven/doxia/module/markdown/MarkdownSink.java b/doxia-modules/doxia-module-markdown/src/main/java/org/apache/maven/doxia/module/markdown/MarkdownSink.java index 7295da73..fa1b138c 100644 --- a/doxia-modules/doxia-module-markdown/src/main/java/org/apache/maven/doxia/module/markdown/MarkdownSink.java +++ b/doxia-modules/doxia-module-markdown/src/main/java/org/apache/maven/doxia/module/markdown/MarkdownSink.java @@ -18,6 +18,8 @@ */ package org.apache.maven.doxia.module.markdown; +import javax.swing.text.MutableAttributeSet; + import java.io.PrintWriter; import java.io.Writer; import java.util.ArrayList; @@ -35,7 +37,9 @@ import org.apache.maven.doxia.sink.Sink; import org.apache.maven.doxia.sink.SinkEventAttributes; import org.apache.maven.doxia.sink.impl.AbstractTextSink; import org.apache.maven.doxia.sink.impl.SinkEventAttributeSet; +import org.apache.maven.doxia.sink.impl.SinkUtils; import org.apache.maven.doxia.sink.impl.Xhtml5BaseSink; +import org.apache.maven.doxia.util.DoxiaUtils; import org.apache.maven.doxia.util.HtmlTools; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -774,13 +778,30 @@ public class MarkdownSink extends AbstractTextSink implements MarkdownMarkup { /** {@inheritDoc} */ public void anchor(String name, SinkEventAttributes attributes) { - // write(ANCHOR_START_MARKUP + name); - // TODO get implementation from Xhtml5 base sink + // emit html anchor as markdown does not support anchors + MutableAttributeSet atts = SinkUtils.filterAttributes(attributes, SinkUtils.SINK_BASE_ATTRIBUTES); + + String id = name; + + if (!DoxiaUtils.isValidId(id)) { + id = DoxiaUtils.encodeId(name); + + LOGGER.debug("{}Modified invalid anchor name '{}' to '{}'", getLocationLogPrefix(), name, id); + } + + MutableAttributeSet att = new SinkEventAttributeSet(); + att.addAttribute(SinkEventAttributes.ID, id); + att.addAttributes(atts); + StringBuilder htmlAnchor = new StringBuilder("<a"); + htmlAnchor.append(SinkUtils.getAttributeString(att)); + htmlAnchor.append(">"); + htmlAnchor.append("</a>"); // close anchor tag immediately otherwise markdown would not be allowed afterwards + writeUnescaped(htmlAnchor.toString()); } @Override public void anchor_() { - // write(ANCHOR_END_MARKUP); + // anchor is always empty html element, i.e. already closed with anchor() } /** {@inheritDoc} */ diff --git a/doxia-modules/doxia-module-markdown/src/test/java/org/apache/maven/doxia/module/markdown/MarkdownSinkTest.java b/doxia-modules/doxia-module-markdown/src/test/java/org/apache/maven/doxia/module/markdown/MarkdownSinkTest.java index 4355dfe1..e3a02636 100644 --- a/doxia-modules/doxia-module-markdown/src/test/java/org/apache/maven/doxia/module/markdown/MarkdownSinkTest.java +++ b/doxia-modules/doxia-module-markdown/src/test/java/org/apache/maven/doxia/module/markdown/MarkdownSinkTest.java @@ -245,7 +245,7 @@ public class MarkdownSinkTest extends AbstractSinkTest { /** {@inheritDoc} */ protected String getAnchorBlock(String anchor) { - return anchor; + return "<a id=\"" + anchor + "\"></a>" + anchor; } /** {@inheritDoc} */