-
-
Notifications
You must be signed in to change notification settings - Fork 183
Markdown support in notification #184
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
Merged
jmattheis
merged 5 commits into
gotify:master
from
Sternagfonkel:feature/#74_unrendered-markdown
Aug 21, 2021
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
90ac114
Removed markdown meta data from notification
bffae44
Merge remote-tracking branch 'refs/remotes/origin/master'
156ca47
Added (limited) support for rendering markdown in notification
cc616a9
Ignore links in notification rendering
e25bf4c
Fixed missing tables in notification
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
119 changes: 119 additions & 0 deletions
119
app/src/main/java/com/github/gotify/MarkwonFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| package com.github.gotify; | ||
|
|
||
| import android.content.Context; | ||
| import android.graphics.Color; | ||
| import android.graphics.Typeface; | ||
| import android.text.style.BackgroundColorSpan; | ||
| import android.text.style.BulletSpan; | ||
| import android.text.style.QuoteSpan; | ||
| import android.text.style.RelativeSizeSpan; | ||
| import android.text.style.StyleSpan; | ||
| import android.text.style.TypefaceSpan; | ||
| import androidx.annotation.NonNull; | ||
| import com.squareup.picasso.Picasso; | ||
| import io.noties.markwon.AbstractMarkwonPlugin; | ||
| import io.noties.markwon.Markwon; | ||
| import io.noties.markwon.MarkwonSpansFactory; | ||
| import io.noties.markwon.MarkwonVisitor; | ||
| import io.noties.markwon.core.CorePlugin; | ||
| import io.noties.markwon.core.CoreProps; | ||
| import io.noties.markwon.ext.strikethrough.StrikethroughPlugin; | ||
| import io.noties.markwon.ext.tables.TableAwareMovementMethod; | ||
| import io.noties.markwon.ext.tables.TablePlugin; | ||
| import io.noties.markwon.image.picasso.PicassoImagesPlugin; | ||
| import io.noties.markwon.movement.MovementMethodPlugin; | ||
| import java.util.Collections; | ||
| import org.commonmark.ext.gfm.tables.TableCell; | ||
| import org.commonmark.ext.gfm.tables.TablesExtension; | ||
| import org.commonmark.node.BlockQuote; | ||
| import org.commonmark.node.Code; | ||
| import org.commonmark.node.Emphasis; | ||
| import org.commonmark.node.Heading; | ||
| import org.commonmark.node.Link; | ||
| import org.commonmark.node.ListItem; | ||
| import org.commonmark.node.StrongEmphasis; | ||
| import org.commonmark.parser.Parser; | ||
|
|
||
| public class MarkwonFactory { | ||
| public static Markwon createForMessage(Context context, Picasso picasso) { | ||
| return Markwon.builder(context) | ||
| .usePlugin(CorePlugin.create()) | ||
| .usePlugin(MovementMethodPlugin.create(TableAwareMovementMethod.create())) | ||
| .usePlugin(PicassoImagesPlugin.create(picasso)) | ||
| .usePlugin(StrikethroughPlugin.create()) | ||
| .usePlugin(TablePlugin.create(context)) | ||
| .build(); | ||
| } | ||
|
|
||
| public static Markwon createForNotification(Context context, Picasso picasso) { | ||
| final float[] headingSizes = { | ||
| 2.F, 1.5F, 1.17F, 1.F, .83F, .67F, | ||
| }; | ||
|
|
||
| final int bulletGapWidth = | ||
| (int) (8 * context.getResources().getDisplayMetrics().density + 0.5F); | ||
|
|
||
| return Markwon.builder(context) | ||
| .usePlugin(CorePlugin.create()) | ||
| .usePlugin(PicassoImagesPlugin.create(picasso)) | ||
| .usePlugin(StrikethroughPlugin.create()) | ||
| .usePlugin( | ||
| new AbstractMarkwonPlugin() { | ||
| @Override | ||
| public void configureSpansFactory( | ||
| @NonNull MarkwonSpansFactory.Builder builder) { | ||
| builder.setFactory( | ||
| Heading.class, | ||
| (configuration, props) -> | ||
| new Object[] { | ||
| new RelativeSizeSpan( | ||
| headingSizes[ | ||
| CoreProps.HEADING_LEVEL | ||
| .require( | ||
| props) | ||
| - 1]), | ||
| new StyleSpan(Typeface.BOLD) | ||
| }) | ||
| .setFactory( | ||
| Emphasis.class, | ||
| (configuration, props) -> | ||
| new StyleSpan(Typeface.ITALIC)) | ||
| .setFactory( | ||
| StrongEmphasis.class, | ||
| (configuration, props) -> | ||
| new StyleSpan(Typeface.BOLD)) | ||
| .setFactory( | ||
| BlockQuote.class, | ||
| (configuration, props) -> new QuoteSpan()) | ||
| .setFactory( | ||
| Code.class, | ||
| (configuration, props) -> | ||
| new Object[] { | ||
| new BackgroundColorSpan(Color.LTGRAY), | ||
| new TypefaceSpan("monospace") | ||
| }) | ||
| .setFactory( | ||
| ListItem.class, | ||
| (configuration, props) -> | ||
| new BulletSpan(bulletGapWidth)) | ||
| .setFactory(Link.class, ((configuration, props) -> null)); | ||
| } | ||
|
|
||
| @Override | ||
| public void configureParser(@NonNull Parser.Builder builder) { | ||
| builder.extensions(Collections.singleton(TablesExtension.create())); | ||
| } | ||
|
|
||
| @Override | ||
| public void configureVisitor(@NonNull MarkwonVisitor.Builder builder) { | ||
| builder.on( | ||
| TableCell.class, | ||
| (visitor, node) -> { | ||
| visitor.visitChildren(node); | ||
| visitor.builder().append(' '); | ||
| }); | ||
| } | ||
| }) | ||
| .build(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.