Skip to content

Commit

Permalink
Create samples by type of markdown rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
jeziellago committed Jan 5, 2024
1 parent 733c9be commit 11587da
Show file tree
Hide file tree
Showing 4 changed files with 260 additions and 292 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal object MarkdownRender {
linkifyMask: Int,
onLinkClicked: ((String) -> Unit)? = null
): Markwon {
val coilImageLoader = imageLoader ?:createCoilImageLoader(context)
val coilImageLoader = imageLoader ?: createCoilImageLoader(context)
return Markwon.builder(context)
.usePlugin(HtmlPlugin.create())
.usePlugin(CoilImagesPlugin.create(context, coilImageLoader))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,6 @@ fun MarkdownText(
setMaxLines(maxLines)
setLinkTextColor(linkTextColor.toArgb())

applyTextColor(style.color.takeOrElse { defaultColor }.toArgb())
applyLineHeight(style)
applyFontSize(style)
applyLineSpacing(style)
applyTextDecoration(style)

style.textAlign?.let { applyTextAlign(it) }
style.fontStyle?.let { applyFontStyle(it) }
style.fontWeight?.let { applyFontWeight(it) }

setTextIsSelectable(isTextSelectable)

movementMethod = LinkMovementMethod.getInstance()
Expand All @@ -147,6 +137,19 @@ fun MarkdownText(
}
},
update = { textView ->
with(textView) {
applyTextColor(style.color.takeOrElse { defaultColor }.toArgb())
applyFontSize(style)
applyLineHeight(style)
applyLineSpacing(style)
applyTextDecoration(style)

with(style) {
textAlign?.let { applyTextAlign(it) }
fontStyle?.let { applyFontStyle(it) }
fontWeight?.let { applyFontWeight(it) }
}
}
markdownRender.setMarkdown(textView, markdown)
if (disableLinkMovementMethod) {
textView.movementMethod = null
Expand Down
274 changes: 246 additions & 28 deletions sample/src/main/java/dev/jeziellago/compose/markdown/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,269 @@ import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.Card
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import dev.jeziellago.compose.markdowntext.MarkdownText

class MainActivity : AppCompatActivity() {

private var rawMarkdown = ""

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
rawMarkdown = String(resources.openRawResource(R.raw.markdown).readBytes()) +
"\n# HTML SECTION ${String(resources.openRawResource(R.raw.html).readBytes())}"
setContent {
SampleMarkdown()
}
setContent { SampleMarkdown() }
}

@Composable
fun SampleMarkdown() {
Card(
modifier = Modifier.fillMaxWidth(),
elevation = 10.dp
LazyColumn(
modifier = Modifier
.padding(8.dp)
.fillMaxWidth()
.clickable {
Toast
.makeText(this@MainActivity, "On text click", Toast.LENGTH_SHORT)
.show()
},
) {
LazyColumn(
modifier = Modifier
.padding(8.dp)
.clickable {
Toast
.makeText(this@MainActivity, "On text click", Toast.LENGTH_SHORT)
.show()
},
) {
item {
MarkdownText(
markdown = rawMarkdown,
viewId =
R.id.markdownTextId,
item {
MarkdownText(
markdown = """
## Justify
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
""".trimIndent(),
style = TextStyle(
textAlign = TextAlign.Justify,
disableLinkMovementMethod = false
)
}
)
}
item {
MarkdownText(
markdown = """
## Color
This text should appears in Blue color.
""".trimIndent(),
style = TextStyle(
color = Color.Blue
)
)
}
item {
MarkdownText(
markdown = """
---
__Advertisement :)__
- __[nodeca](https://nodeca.github.io/pica/demo/)__ - high quality and fast image
resize in browser.
- __[babelfish](https://github.com/nodeca/babelfish/)__ - developer friendly
i18n with plurals support and easy syntax.
You will like those projects!
---
""".trimIndent(),
)
}
item {
MarkdownText(
markdown = """
# h1 Heading 8-)
## h2 Heading
### h3 Heading
#### h4 Heading
##### h5 Heading
###### h6 Heading
""".trimIndent(),
)
}
item {
MarkdownText(
markdown = """
## Horizontal Rules
___
---
***
""".trimIndent(),
)
}
item {
MarkdownText(
markdown = """
## Emphasis
**This is bold text**
__This is bold text__
*This is italic text*
_This is italic text_
~~Strikethrough~~
""".trimIndent(),
)
}
item {
MarkdownText(
markdown = """
## Links
[link text](http://dev.nodeca.com)
[link with title](http://nodeca.github.io/pica/demo/ "title text!")
Autoconverted link https://github.com/nodeca/pica (enable linkify to see)
""".trimIndent(),
)
}
item {
MarkdownText(
modifier = Modifier.fillMaxWidth(),
markdown = """
## Images
![Minion](https://octodex.github.com/images/minion.png)
![Stormtroopocat](https://octodex.github.com/images/stormtroopocat.jpg "The Stormtroopocat")
""".trimIndent(),
)
}
item {
MarkdownText(
modifier = Modifier.fillMaxWidth(),
markdown = """
Content
with
line
break
""".trimIndent(),
)
}
item {
MarkdownText(
modifier = Modifier.fillMaxWidth(),
markdown = """
## Tables
| Option | Description |
| ------ | ----------- |
| data | path to data files to supply the data that will be passed into templates. |
| engine | engine to be used for processing templates. Handlebars is the default. |
| ext | extension to be used for dest files. |
Right aligned columns
| Option | Description |
| ------:| -----------:|
| data | path to data files to supply the data that will be passed into templates. |
| engine | engine to be used for processing templates. Handlebars is the default. |
| ext | extension to be used for dest files. |
""".trimIndent(),
)
}
item {
MarkdownText(
markdown = """
## Lists
Unordered
+ Create a list by starting a line with `+`, `-`, or `*`
+ Sub-lists are made by indenting 2 spaces:
- Marker character change forces new list start:
* Ac tristique libero volutpat at
+ Facilisis in pretium nisl aliquet
- Nulla volutpat aliquam velit
+ Very easy!
Ordered
1. Lorem ipsum dolor sit amet
2. Consectetur adipiscing elit
3. Integer molestie lorem at massa
1. You can use sequential numbers...
1. ...or keep all the numbers as `1.`
Start numbering with offset:
57. foo
1. bar
""".trimIndent(),
)
}
item {
MarkdownText(
markdown = """
## Blockquotes
> Blockquotes can also be nested...
>> ...by using additional greater-than signs right next to each other...
> > > ...or with spaces between arrows.
""".trimIndent(),
)
}
item {
MarkdownText(
markdown = """
## Code
Inline `code`
Indented code
// Some comments
line 1 of code
line 2 of code
line 3 of code
Block code "fences"
```
Sample text here...
```
more code here..
``` js
var foo = function (bar) {
return bar++;
};
console.log(foo(5));
```
""".trimIndent(),
)
}
item {
MarkdownText(
markdown = "\n# HTML SECTION ${
String(
resources.openRawResource(R.raw.html).readBytes()
)
}",
)
}
}
}
}
}
Loading

0 comments on commit 11587da

Please sign in to comment.