Skip to content

Commit

Permalink
Fix: Code block with single word case (#21)
Browse files Browse the repository at this point in the history
This has a side effect of making inlined code blocks require a newline to register the language.
However this caveat is also the same as the code rules we use on discord desktop.
  • Loading branch information
lytefast authored Nov 11, 2020
1 parent 829f6b1 commit 29c77ec
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
5 changes: 3 additions & 2 deletions app/src/main/java/com/discord/simpleast/sample/SampleTexts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,9 @@ object SampleTexts {

const val CODE_BLOCKS = """
# Code block samples
inlined:```kt private fun test() {}```
inlined:```kt private fun test() {
inlined:```py language code blocks need newline```
inlined:```kt
private fun test() {
some.call()
}```
Expand Down
2 changes: 1 addition & 1 deletion simpleast-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {
minSdkVersion 21
targetSdkVersion 30
versionCode 28
versionName "2.1.1"
versionName "2.1.2"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ object CodeRules {
* Handles markdown syntax for code blocks for a given language.
*
* Examples:
* inlined ```kt fun test()```
* inlined2 ```kt
* inlined ```test```
* inlined ```kt language code blocks need newline```
* inlined block start ```kt
* fun test()
* ```
*
Expand All @@ -37,7 +38,7 @@ object CodeRules {
* ```
*/
val PATTERN_CODE_BLOCK: Pattern =
Pattern.compile("""^```(?:([A-z0-9_+\-.]+))?(\s*)([^\n].*?)\n*```""", Pattern.DOTALL)
Pattern.compile("""^```(?:([\w+\-.]+?)(\s*\n))?([^\n].*?)\n*```""", Pattern.DOTALL)

val PATTERN_CODE_INLINE: Pattern =
Pattern.compile("""^`(?:\s*)([^\n].*?)\n*`""", Pattern.DOTALL)
Expand Down Expand Up @@ -226,7 +227,7 @@ object CodeRules {
: ParseSpec<R, S> {
val language = matcher.group(CODE_BLOCK_LANGUAGE_GROUP)
val codeBody = matcher.group(CODE_BLOCK_BODY_GROUP).orEmpty()
val startsWithNewline = matcher.group(CODE_BLOCK_WS_PREFIX)!!.contains('\n')
val startsWithNewline = matcher.group(CODE_BLOCK_WS_PREFIX)?.contains('\n') ?: false

val languageRules = language?.let { languageMap[it] }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ class CodeRulesTest {
treeMatcher.registerDefaultMatchers()
}

@Test
fun noLanguageOneLined() {
val ast = parser.parse("""
```code```
```spaces ```
```some text```
""".trimIndent(), TestState())

ast.assertNodeContents<CodeNode<*>>("code", "spaces ", "some text")
}

@Test
fun commentsRust() {
val ast = parser.parse("""
Expand Down

0 comments on commit 29c77ec

Please sign in to comment.