diff --git a/CHANGELOG.md b/CHANGELOG.md index 95c04c1f..047ae4d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ ``` * `JLatexMathPlugin`: add `theme` (to customize both inlines and blocks) * `JLatexMathPlugin`: add `renderMode` to use previous (pre `4.3.0`) LaTeX rendering +* add `SoftBreakAddsNewLinePlugin` plugin (`core` module) + # 4.2.2 * Fixed `AsyncDrawable` display when it has placeholder with empty bounds ([#189]) diff --git a/markwon-core/src/main/java/io/noties/markwon/SoftBreakAddsNewLinePlugin.java b/markwon-core/src/main/java/io/noties/markwon/SoftBreakAddsNewLinePlugin.java new file mode 100644 index 00000000..6f49ac68 --- /dev/null +++ b/markwon-core/src/main/java/io/noties/markwon/SoftBreakAddsNewLinePlugin.java @@ -0,0 +1,26 @@ +package io.noties.markwon; + +import androidx.annotation.NonNull; + +import org.commonmark.node.SoftLineBreak; + +/** + * @since 4.3.0-SNAPSHOT + */ +public class SoftBreakAddsNewLinePlugin extends AbstractMarkwonPlugin { + + @NonNull + public static SoftBreakAddsNewLinePlugin create() { + return new SoftBreakAddsNewLinePlugin(); + } + + @Override + public void configureVisitor(@NonNull MarkwonVisitor.Builder builder) { + builder.on(SoftLineBreak.class, new MarkwonVisitor.NodeVisitor() { + @Override + public void visit(@NonNull MarkwonVisitor visitor, @NonNull SoftLineBreak softLineBreak) { + visitor.forceNewLine(); + } + }); + } +} diff --git a/sample/src/main/java/io/noties/markwon/sample/basicplugins/BasicPluginsActivity.java b/sample/src/main/java/io/noties/markwon/sample/basicplugins/BasicPluginsActivity.java index 7fe69cd3..49703e72 100644 --- a/sample/src/main/java/io/noties/markwon/sample/basicplugins/BasicPluginsActivity.java +++ b/sample/src/main/java/io/noties/markwon/sample/basicplugins/BasicPluginsActivity.java @@ -21,6 +21,7 @@ import io.noties.markwon.Markwon; import io.noties.markwon.MarkwonConfiguration; import io.noties.markwon.MarkwonSpansFactory; import io.noties.markwon.MarkwonVisitor; +import io.noties.markwon.SoftBreakAddsNewLinePlugin; import io.noties.markwon.core.MarkwonTheme; import io.noties.markwon.image.ImageItem; import io.noties.markwon.image.ImagesPlugin; @@ -35,15 +36,17 @@ public class BasicPluginsActivity extends ActivityWithMenuOptions { private TextView textView; - @NonNull @Override public MenuOptions menuOptions() { return MenuOptions.create() .add("paragraphSpan", this::paragraphSpan) .add("disableNode", this::disableNode) + .add("customizeTheme", this::customizeTheme) .add("linkWithMovementMethod", this::linkWithMovementMethod) - .add("imagesPlugin", this::imagesPlugin); + .add("imagesPlugin", this::imagesPlugin) + .add("softBreakAddsSpace", this::softBreakAddsSpace) + .add("softBreakAddsNewLine", this::softBreakAddsNewLine); } @Override @@ -217,6 +220,28 @@ public class BasicPluginsActivity extends ActivityWithMenuOptions { markwon.setMarkdown(textView, markdown); } + private void softBreakAddsSpace() { + // default behavior + + final String md = "" + + "Hello there ->(line)\n(break)<- going on and on"; + + Markwon.create(this).setMarkdown(textView, md); + } + + private void softBreakAddsNewLine() { + // insert a new line when markdown has a soft break + + final Markwon markwon = Markwon.builder(this) + .usePlugin(SoftBreakAddsNewLinePlugin.create()) + .build(); + + final String md = "" + + "Hello there ->(line)\n(break)<- going on and on"; + + markwon.setMarkdown(textView, md); + } + // public void step_6() { // // final Markwon markwon = Markwon.builder(this)