added extensive markdown rendering tests and Kotlin/JS externals for marked library

This commit is contained in:
sergeych committed 2025-11-18 23:10:22 +01:00
1 parent 67e4d76f59
commit f4d1a77496
10 files changed
+848 -20

No files matched your search

@@ -0,0 +1,40 @@
/*
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/*
* Test that markdown fenced code blocks render with `.code` class on <pre>
*/
import kotlin.test.Test
import kotlin.test.assertTrue
class CodeBlockStyleTest {
@Test
fun codeBlocksGetBootstrapClass() {
val md = """
```kotlin
println("Hi")
```
""".trimIndent()
val html = renderMarkdown(md)
assertTrue(html.contains("<pre", ignoreCase = true), "Rendered HTML should contain a <pre> tag. Got: $html")
val hasClass = html.contains("<pre class=\"code\"", ignoreCase = true) ||
html.contains(" class=\"code ", ignoreCase = true) ||
html.contains(" class=\"code\"", ignoreCase = true) ||
Regex("""<pre[^>]*class=['"][^'"]*\bcode\b[^'"]*['"]""", RegexOption.IGNORE_CASE).containsMatchIn(html)
assertTrue(hasClass, "<pre> should have 'code' class. Got: $html")
}
}
@@ -0,0 +1,55 @@
/*
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/*
* Tests for definition list post-processing
*/
import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue
class DefinitionListTest {
@Test
fun singleTermMultipleDefinitions() {
val md = """
Term
: First definition
: Second definition with an [inline link](#here)
""".trimIndent()
val html = renderMarkdown(md)
assertTrue(html.contains("<dl>", ignoreCase = true), "Expected <dl> in rendered HTML. Got: $html")
assertTrue(html.contains("<dt>Term</dt>", ignoreCase = true), "Term should be inside <dt>. Got: $html")
// There should be two <dd> entries
val ddCount = Regex("<dd>", RegexOption.IGNORE_CASE).findAll(html).count()
assertTrue(ddCount == 2, "Expected two <dd> elements, got $ddCount. HTML: $html")
// No leading ':' should remain inside definitions
assertFalse(Regex("<dd>\\s*:", RegexOption.IGNORE_CASE).containsMatchIn(html), "Definition should not start with ':'. HTML: $html")
}
@Test
fun notADefListWhenStartsWithColon() {
val md = """
: Not a term paragraph
Next paragraph
""".trimIndent()
val html = renderMarkdown(md)
// Should not produce a <dl>
assertFalse(html.contains("<dl>", ignoreCase = true), "Should not create <dl> when first paragraph starts with ':'. HTML: $html")
}
}
@@ -0,0 +1,31 @@
/*
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/*
* Basic test to ensure markdown is actually rendered by the ESM `marked` import
*/
import kotlin.test.Test
import kotlin.test.assertTrue
class MarkdownRenderTest {
@Test
fun rendersHeading() {
val html = renderMarkdown("# Hello")
assertTrue(html.contains("<h1", ignoreCase = true), "Expected <h1> in rendered HTML, got: $html")
assertTrue(html.contains("Hello"), "Rendered HTML should contain the heading text")
}
}
@@ -0,0 +1,46 @@
/*
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/*
* Tests for Reference page rendering helpers
*/
import kotlin.test.Test
import kotlin.test.assertTrue
class ReferencePageTest {
@Test
fun rendersReferenceListWithLinks() {
val docs = listOf(
"docs/Iterator.md",
"docs/guides/perf_guide.md"
)
val html = renderReferenceListHtml(docs)
// Basic structure
assertTrue(html.contains("<ul", ignoreCase = true), "Expected <ul> in reference list HTML: $html")
// Contains links to the docs routes
assertTrue(html.contains("href=\"#/docs/Iterator.md\""), "Should link to #/docs/Iterator.md: $html")
assertTrue(html.contains("Iterator.md"), "Should display file name Iterator.md: $html")
// Nested path should display directory info
assertTrue(html.contains("guides"), "Should include directory name for nested docs: $html")
// Chevron icon present
assertTrue(html.contains("bi-chevron-right"), "Should include chevron icon class: $html")
}
}
@@ -0,0 +1,48 @@
/*
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/*
* Tests for route/anchor parsing utilities to support TOC navigation.
*/
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
class RouteParsingTest {
@Test
fun stripFragmentRemovesAnchor() {
assertEquals("docs/Iterator.md", stripFragment("docs/Iterator.md#sec"))
assertEquals("docs/Iterator.md", stripFragment("docs/Iterator.md"))
assertEquals("docs/dir/file.md", stripFragment("docs/dir/file.md#x-y"))
}
@Test
fun routeToPathDropsFragmentAndNormalizes() {
assertEquals("docs/Iterator.md", routeToPath("docs/Iterator.md#part"))
assertEquals("docs/Iterator.md", routeToPath("Iterator.md#part"))
assertEquals("docs/guides/perf_guide.md", routeToPath("guides/perf_guide.md#toc"))
}
@Test
fun anchorFromHashParsesSecondHash() {
assertEquals("part", anchorFromHash("#/docs/Iterator.md#part"))
assertEquals("sec-2", anchorFromHash("#/docs/g/it.md#sec-2"))
assertNull(anchorFromHash("#/docs/Iterator.md"))
assertNull(anchorFromHash("#just-a-frag"))
assertNull(anchorFromHash("/not-a-hash"))
}
}
+37
View File
@@ -0,0 +1,37 @@
/*
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/*
* Test that markdown tables are rendered with Bootstrap `.table` class
*/
import kotlin.test.Test
import kotlin.test.assertTrue
class TableStyleTest {
@Test
fun tablesGetBootstrapClass() {
val md = """
|Col A|Col B|
|-----|-----|
| 1 | 2 |
""".trimMargin()
val html = renderMarkdown(md)
assertTrue(html.contains("<table", ignoreCase = true), "Rendered HTML should contain a <table> tag. Got: $html")
assertTrue(html.contains("class=\"table\"") || html.contains("class=\"table ") || html.contains(" class=\"table\""),
"Rendered <table> should have Bootstrap 'table' class. Got: $html")
}
}