feat(android): markdown/code/diff rendering foundation (B-M2d1)

:core:protocol (pure JVM, all web-transform ports fixture-style tested):
- markdown/MarkdownTransforms.kt: repairTables (remark-repair-tables port),
  disableIndentedCode (enabledBlockTypes minus IndentedCodeBlock == micromark
  disable codeIndented) + canonical parser factory (GFM tables, strikethrough
  requireTwoTildes, autolink), stripCjkAutolinkArtifacts, detectFilePathLinks/
  matchWholeFilePath/rewriteExplicitLinkTarget (remark-file-path-links port)
- markdown/HrefPolicy.kt: Allowed|ConfirmFirst|Blocked scheme policy with
  double-percent-decode + control-char-strip bypass hardening (web parity);
  scheme-less/SPA/protocol-relative fail closed on native
- git/UnifiedDiffParser.kt: full unified diff -> DiffFile/DiffHunk/DiffLine
  (rename/copy/new/deleted/binary/mode/similarity, quoted paths, bare codex
  diffs, tolerant of truncated hunks + wrong LLM counts)
- 56 new JVM tests (repair matrix, parser config, CJK, path detection incl.
  negatives, href matrix, 11 diff scenarios cross-checked vs web semantics)

:app Compose UI:
- ui/markdown/Markdown.kt: transforms -> commonmark AST walk; paragraphs/
  headings/lists (ordered/bullet/task via stripped text markers)/blockquote/
  tables (intrinsic-width grid in horizontal scroll)/thematic break/html
  monospace fallback; unknown fences (mermaid/math) degrade to CodeBlock;
  LinkAnnotation links via LocalMarkdownLinkHandler (onFilePath/onUrl+policy)
- ui/markdown/CodeBlock.kt: header chip + copy, horizontal scroll, highlights
  1.0.0 syntax coloring off-main via produceState with 200-entry LRU keyed by
  (hash, len, lang, dark), 400-line cap then plain
- ui/components/DiffView.kt: hunk headers, +/- tinted rows, dual line-number
  gutters, compact/expand
- ui/theme: HapiExtendedColors (code/diff/table/quote tokens mirroring web
  index.css light/dark/OLED) + pure-black OLED scheme, provided by HapiTheme
- MarkdownPreviewParameterProvider + kitchen-sink @Previews (compile-checked)

deps (Maven Central verified): org.commonmark:commonmark 0.24.0 + gfm-tables/
gfm-strikethrough/autolink exts, dev.snipme:highlights 1.0.0 (jvm variant)

Verified: :core:protocol:test 141 green, :app:assembleDebug green
This commit is contained in:
weishu
2026-08-17 15:14:01 +08:00
parent 1e80327d76
commit d39197b97d
17 changed files with 3109 additions and 5 deletions
@@ -0,0 +1,416 @@
package app.hapi.protocol.git
/**
* Unified diff parser for the raw `git diff` stdout the hub relays verbatim
* (`{success, stdout, stderr}` -- clients parse themselves, like
* `web/src/lib/gitParsers.ts` does for status/numstat) and for agent-supplied
* `unified_diff` tool inputs (the web reference for line semantics is
* `parseUnifiedDiff` in `web/src/components/ToolCard/views/CodexDiffView.tsx`:
* context lines belong to both sides, `\ No newline at end of file` markers are
* not content, blank lines inside a hunk are empty context lines).
*
* Handles `diff --git` headers (new/deleted/rename/copy/mode/similarity/index/
* binary markers) as well as bare `--- / +++ / @@` diffs without a git header.
* Tolerant of streaming-truncated hunks and of the wrong `@@` counts LLMs emit:
* counts steer disambiguation but never discard lines.
*/
enum class DiffLineKind { CONTEXT, ADD, REMOVE }
data class DiffLine(
val kind: DiffLineKind,
/** 1-based line number in the old file; null for ADD lines. */
val oldLineNumber: Int?,
/** 1-based line number in the new file; null for REMOVE lines. */
val newLineNumber: Int?,
/** Line content without the +/-/space marker. */
val text: String,
/** True when a `\ No newline at end of file` marker followed this line. */
val noNewlineAtEnd: Boolean = false,
)
data class DiffHunk(
/** The raw `@@ -a,b +c,d @@` header line (heading included). */
val header: String,
val oldStart: Int,
val oldCount: Int,
val newStart: Int,
val newCount: Int,
/** Text after the closing `@@` (enclosing function/section), if any. */
val sectionHeading: String?,
val lines: List<DiffLine>,
) {
val additions: Int get() = lines.count { it.kind == DiffLineKind.ADD }
val deletions: Int get() = lines.count { it.kind == DiffLineKind.REMOVE }
}
enum class DiffChangeKind { MODIFY, ADD, DELETE, RENAME, COPY }
data class DiffFile(
/** Old path (`a/` prefix stripped); null for created files (`/dev/null`). */
val oldPath: String?,
/** New path (`b/` prefix stripped); null for deleted files (`/dev/null`). */
val newPath: String?,
val changeKind: DiffChangeKind,
val isBinary: Boolean,
val oldMode: String?,
val newMode: String?,
/** `similarity index N%` for renames/copies. */
val similarity: Int?,
val hunks: List<DiffHunk>,
) {
/** Path to show: the post-image, falling back to the pre-image. */
val displayPath: String get() = newPath ?: oldPath ?: ""
val additions: Int get() = hunks.sumOf { it.additions }
val deletions: Int get() = hunks.sumOf { it.deletions }
}
object UnifiedDiffParser {
private val HUNK_HEADER = Regex("""^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@(.*)$""")
private val DIFF_GIT = Regex("""^diff --git (.+)$""")
private val SIMILARITY = Regex("""^similarity index (\d{1,3})%$""")
private val DISSIMILARITY = Regex("""^dissimilarity index (\d{1,3})%$""")
private val OLD_MODE = Regex("""^old mode (\d{6})$""")
private val NEW_MODE = Regex("""^new mode (\d{6})$""")
private val NEW_FILE_MODE = Regex("""^new file mode (\d{6})$""")
private val DELETED_FILE_MODE = Regex("""^deleted file mode (\d{6})$""")
private val INDEX_LINE = Regex("""^index [0-9a-f]+\.\.[0-9a-f]+( \d{6})?$""")
private val BINARY_FILES = Regex("""^Binary files (.+) and (.+) differ$""")
fun parse(diffOutput: String): List<DiffFile> {
val lines = diffOutput.split("\n")
val files = mutableListOf<DiffFile>()
var file: FileBuilder? = null
var hunk: HunkBuilder? = null
fun closeHunk() {
val h = hunk ?: return
file?.hunks?.add(h.build())
hunk = null
}
fun closeFile() {
closeHunk()
val f = file ?: return
files.add(f.build())
file = null
}
var i = 0
while (i < lines.size) {
val line = lines[i]
val inHunk = hunk != null
// ── Hunk content (checked first: `--- x` inside a hunk is a REMOVE) ──
if (inHunk) {
val h = hunk!!
val consumed = when {
line.startsWith("\\") -> {
// `\ No newline at end of file` annotates the previous line.
h.markNoNewline()
true
}
h.wantsMore() -> h.consume(line)
// Counts are exhausted: only clearly-content lines continue the
// hunk (tolerates wrong LLM counts); structure lines close it.
line.startsWith("@@") || isFileBoundary(lines, i) -> false
line.startsWith("+") || line.startsWith("-") || line.startsWith(" ") ->
h.consume(line)
else -> false
}
if (consumed) {
i += 1
continue
}
closeHunk()
// fall through: re-process this line as structure
}
val hunkMatch = HUNK_HEADER.find(line)
when {
hunkMatch != null -> {
if (file == null) file = FileBuilder() // hunk with no header at all
val oldStart = hunkMatch.groupValues[1].toIntOrNull() ?: 0
val oldCount = hunkMatch.groupValues[2].ifEmpty { "1" }.toIntOrNull() ?: 1
val newStart = hunkMatch.groupValues[3].toIntOrNull() ?: 0
val newCount = hunkMatch.groupValues[4].ifEmpty { "1" }.toIntOrNull() ?: 1
val heading = hunkMatch.groupValues[5].removePrefix(" ").ifEmpty { null }
hunk = HunkBuilder(line, oldStart, oldCount, newStart, newCount, heading)
}
DIFF_GIT.matches(line) -> {
closeFile()
file = FileBuilder()
val paths = parseDiffGitPaths(DIFF_GIT.find(line)!!.groupValues[1])
if (paths != null) {
file!!.headerOldPath = paths.first
file!!.headerNewPath = paths.second
}
}
line.startsWith("--- ") -> {
// A bare `---`/`+++` pair (no `diff --git`) starts a new file.
if (file != null && file!!.sawMinusPath) closeFile()
if (file == null) file = FileBuilder()
file!!.oldPath = parseFileHeaderPath(line.removePrefix("--- "), "a/")
file!!.sawMinusPath = true
}
line.startsWith("+++ ") -> {
if (file == null) file = FileBuilder()
file!!.newPath = parseFileHeaderPath(line.removePrefix("+++ "), "b/")
file!!.sawPlusPath = true
}
file != null && SIMILARITY.matches(line) ->
file!!.similarity = SIMILARITY.find(line)!!.groupValues[1].toIntOrNull()
file != null && DISSIMILARITY.matches(line) -> Unit
file != null && OLD_MODE.matches(line) ->
file!!.oldMode = OLD_MODE.find(line)!!.groupValues[1]
file != null && NEW_MODE.matches(line) ->
file!!.newMode = NEW_MODE.find(line)!!.groupValues[1]
file != null && NEW_FILE_MODE.matches(line) -> {
file!!.isNewFile = true
file!!.newMode = NEW_FILE_MODE.find(line)!!.groupValues[1]
}
file != null && DELETED_FILE_MODE.matches(line) -> {
file!!.isDeletedFile = true
file!!.oldMode = DELETED_FILE_MODE.find(line)!!.groupValues[1]
}
file != null && line.startsWith("rename from ") -> {
file!!.isRename = true
file!!.renameFrom = line.removePrefix("rename from ")
}
file != null && line.startsWith("rename to ") -> {
file!!.isRename = true
file!!.renameTo = line.removePrefix("rename to ")
}
file != null && line.startsWith("copy from ") -> {
file!!.isCopy = true
file!!.renameFrom = line.removePrefix("copy from ")
}
file != null && line.startsWith("copy to ") -> {
file!!.isCopy = true
file!!.renameTo = line.removePrefix("copy to ")
}
file != null && INDEX_LINE.matches(line) -> Unit
BINARY_FILES.matches(line) -> {
if (file == null) file = FileBuilder()
file!!.isBinary = true
}
line == "GIT binary patch" -> {
if (file == null) file = FileBuilder()
file!!.isBinary = true
}
else -> Unit // prologue/epilogue noise (e.g. `warning:` lines)
}
i += 1
}
closeFile()
return files
}
/** True when line i starts a new file section (`diff --git`, `Binary files`, or a `---`/`+++` pair). */
private fun isFileBoundary(lines: List<String>, i: Int): Boolean {
val line = lines[i]
if (DIFF_GIT.matches(line)) return true
if (BINARY_FILES.matches(line)) return true
return line.startsWith("--- ") && i + 1 < lines.size && lines[i + 1].startsWith("+++ ")
}
/**
* `--- a/path`-style header payload -> path. Strips the customary prefix,
* a trailing `\t`-separated timestamp, and quoting; `/dev/null` -> null.
*/
private fun parseFileHeaderPath(rawValue: String, prefix: String): String? {
var value = rawValue
val tab = value.indexOf('\t')
if (tab >= 0) value = value.substring(0, tab)
value = unquote(value)
if (value == "/dev/null") return null
if (value.startsWith(prefix)) value = value.removePrefix(prefix)
return value
}
/**
* `diff --git a/<old> b/<new>` payload -> (old, new). Handles quoted paths;
* for unquoted paths with spaces uses the last ` b/` separator heuristic.
* Returns null when the payload cannot be split confidently.
*/
private fun parseDiffGitPaths(payload: String): Pair<String, String>? {
if (payload.startsWith("\"")) {
val closing = findClosingQuote(payload) ?: return null
val old = unquote(payload.substring(0, closing + 1))
val rest = payload.substring(closing + 1).trimStart()
val new = unquote(rest)
return stripPrefix(old, "a/") to stripPrefix(new, "b/")
}
val sep = payload.lastIndexOf(" b/")
if (sep < 0) {
// `--no-prefix` form: single space split, best effort.
val space = payload.indexOf(' ')
if (space < 0) return null
return payload.substring(0, space) to payload.substring(space + 1)
}
val old = stripPrefix(payload.substring(0, sep), "a/")
val new = payload.substring(sep + 3)
return old to new
}
private fun stripPrefix(value: String, prefix: String): String =
if (value.startsWith(prefix)) value.removePrefix(prefix) else value
private fun findClosingQuote(quoted: String): Int? {
var i = 1
while (i < quoted.length) {
when (quoted[i]) {
'\\' -> i += 2
'"' -> return i
else -> i += 1
}
}
return null
}
/** Undo git's C-style path quoting (`"a/sp ace.txt"`, `\t`, `\\`, `\"`). */
private fun unquote(value: String): String {
if (!value.startsWith("\"") || !value.endsWith("\"") || value.length < 2) return value
val inner = value.substring(1, value.length - 1)
val sb = StringBuilder(inner.length)
var i = 0
while (i < inner.length) {
val c = inner[i]
if (c == '\\' && i + 1 < inner.length) {
when (val next = inner[i + 1]) {
'n' -> sb.append('\n')
't' -> sb.append('\t')
'r' -> sb.append('\r')
'\\', '"' -> sb.append(next)
else -> {
sb.append(c)
sb.append(next)
}
}
i += 2
} else {
sb.append(c)
i += 1
}
}
return sb.toString()
}
private class FileBuilder {
var headerOldPath: String? = null
var headerNewPath: String? = null
var oldPath: String? = null
var newPath: String? = null
var sawMinusPath = false
var sawPlusPath = false
var isNewFile = false
var isDeletedFile = false
var isRename = false
var isCopy = false
var isBinary = false
var renameFrom: String? = null
var renameTo: String? = null
var oldMode: String? = null
var newMode: String? = null
var similarity: Int? = null
val hunks = mutableListOf<DiffHunk>()
fun build(): DiffFile {
val resolvedOld = when {
isNewFile -> null
sawMinusPath -> oldPath
else -> renameFrom ?: headerOldPath
}
val resolvedNew = when {
isDeletedFile -> null
sawPlusPath -> newPath
else -> renameTo ?: headerNewPath
}
val kind = when {
isRename -> DiffChangeKind.RENAME
isCopy -> DiffChangeKind.COPY
isNewFile || (sawMinusPath && oldPath == null) -> DiffChangeKind.ADD
isDeletedFile || (sawPlusPath && newPath == null) -> DiffChangeKind.DELETE
else -> DiffChangeKind.MODIFY
}
return DiffFile(
oldPath = if (kind == DiffChangeKind.ADD) null else resolvedOld,
newPath = if (kind == DiffChangeKind.DELETE) null else resolvedNew,
changeKind = kind,
isBinary = isBinary,
oldMode = oldMode,
newMode = newMode,
similarity = similarity,
hunks = hunks.toList(),
)
}
}
private class HunkBuilder(
val header: String,
val oldStart: Int,
val oldCount: Int,
val newStart: Int,
val newCount: Int,
val sectionHeading: String?,
) {
private val lines = mutableListOf<DiffLine>()
private var oldNo = oldStart
private var newNo = newStart
private var remainingOld = oldCount
private var remainingNew = newCount
fun wantsMore(): Boolean = remainingOld > 0 || remainingNew > 0
/** Returns true when the line was hunk content. */
fun consume(line: String): Boolean {
when {
line.startsWith("+") -> {
lines += DiffLine(DiffLineKind.ADD, null, newNo, line.substring(1))
newNo += 1
remainingNew -= 1
}
line.startsWith("-") -> {
lines += DiffLine(DiffLineKind.REMOVE, oldNo, null, line.substring(1))
oldNo += 1
remainingOld -= 1
}
line.startsWith(" ") -> {
lines += DiffLine(DiffLineKind.CONTEXT, oldNo, newNo, line.substring(1))
oldNo += 1
newNo += 1
remainingOld -= 1
remainingNew -= 1
}
line.isEmpty() && wantsMore() -> {
// Some generators emit truly empty context lines (web parser
// treats them as empty context on both sides).
lines += DiffLine(DiffLineKind.CONTEXT, oldNo, newNo, "")
oldNo += 1
newNo += 1
remainingOld -= 1
remainingNew -= 1
}
else -> return false
}
return true
}
fun markNoNewline() {
val last = lines.lastOrNull() ?: return
lines[lines.size - 1] = last.copy(noNewlineAtEnd = true)
}
fun build(): DiffHunk = DiffHunk(
header = header,
oldStart = oldStart,
oldCount = oldCount,
newStart = newStart,
newCount = newCount,
sectionHeading = sectionHeading,
lines = lines.toList(),
)
}
}
@@ -0,0 +1,128 @@
package app.hapi.protocol.markdown
import java.io.ByteArrayOutputStream
/**
* Fail-closed policy for markdown link destinations, ported from the web
* client's scheme policy (`web/src/components/assistant-ui/markdown-text.tsx`)
* and scheme-less href policy (`web/src/lib/markdown-href-policy.ts`).
*
* Product rule (#1452): never paint a clickable control that dead-ends.
* Native mapping of the web decisions:
* - IANA-safe schemes (`https` ...) -> [HrefDecision.Allowed]: open directly.
* - Custom schemes (`vscode:` ...) -> [HrefDecision.ConfirmFirst]: confirm dialog,
* optionally "always allow" per scheme (persisted by the app, not here).
* - Deny-listed schemes / scheme-less rest -> [HrefDecision.Blocked]: inert text.
*
* Scheme-less targets differ from the web on purpose: the SPA-route branch
* (`/settings`, `#anchor`, protocol-relative `//host`) has no native equivalent,
* so those fall to Blocked. Workspace-file targets are the caller's job:
* run [MarkdownTransforms.rewriteExplicitLinkTarget] BEFORE [classify]; anything
* that survives to classify() without a scheme is inert.
*/
sealed interface HrefDecision {
/** IANA-registered safe scheme: open without ceremony. */
data object Allowed : HrefDecision
/** Unknown custom scheme: ask the user before dispatching. */
data class ConfirmFirst(val scheme: String) : HrefDecision
/** Deny-listed or unresolvable: render as inert text, never dispatch. */
data object Blocked : HrefDecision
}
object HrefPolicy {
/** Mirrors react-markdown's `defaultUrlTransform` allowlist exactly. */
val IANA_SAFE_SCHEMES: Set<String> = setOf("http", "https", "irc", "ircs", "mailto", "xmpp")
/** Always blocked regardless of user preference. */
val DENY_SCHEMES: Set<String> = setOf("javascript", "data", "vbscript", "file")
private val SCHEME_STRIP = Regex("""[\x00-\x1F\x7F\s]""")
/**
* Extract the normalised scheme from a URL string.
*
* Applies up to two rounds of percent-decoding so double-encoded bypasses
* (`javascript%253A` -> `javascript%3A` -> `javascript:`) are unwrapped, then
* strips ASCII control characters and whitespace from the scheme name --
* platform URL handling silently discards those during dispatch, so
* `java\nscript:alert(1)` must classify as `javascript`.
*
* Returns null when no scheme separator is found.
*/
fun normalizedScheme(url: String): String? {
var value = url.trimStart()
repeat(2) {
val next = percentDecodeOrNull(value) ?: return@repeat
if (next == value) return@repeat
value = next
}
val colonIndex = value.indexOf(':')
if (colonIndex <= 0) return null
return SCHEME_STRIP.replace(value.take(colonIndex), "").lowercase()
}
/**
* True when [href] carries a URL scheme -- a `scheme:` prefix appearing before
* any `/`, `?` or `#`. Distinguishes `mailto:x` (true) from relative targets
* like `docs/a.md`, `#section`, or `/path:colon` (false).
*/
fun hasScheme(href: String): Boolean {
val colonIdx = href.indexOf(':')
if (colonIdx <= 0) return false
val boundaryIdx = href.indexOfFirst { it == '/' || it == '?' || it == '#' }
return boundaryIdx < 0 || colonIdx < boundaryIdx
}
/**
* Classify a markdown link destination. Callers handle workspace file paths
* first (see [MarkdownTransforms.rewriteExplicitLinkTarget]); everything else
* flows through here.
*/
fun classify(url: String): HrefDecision {
val trimmed = url.trim()
if (trimmed.isEmpty()) return HrefDecision.Blocked
// Windows drive paths look like a `c:` scheme; they are file candidates
// owned by the file-path layer, never confirmable custom schemes.
if (MarkdownTransforms.isWindowsAbsolutePath(trimmed)) return HrefDecision.Blocked
// Scheme-less: no SPA router on native -- fail closed to inert.
if (!hasScheme(trimmed)) return HrefDecision.Blocked
val scheme = normalizedScheme(trimmed) ?: return HrefDecision.Blocked
return when {
scheme in DENY_SCHEMES -> HrefDecision.Blocked
scheme in IANA_SAFE_SCHEMES -> HrefDecision.Allowed
else -> HrefDecision.ConfirmFirst(scheme)
}
}
/**
* JS `decodeURIComponent` equivalent: strict `%HH` decoding (no `+` -> space),
* null on malformed escapes so the caller keeps the previous value, mirroring
* the web's try/catch-break loop.
*/
private fun percentDecodeOrNull(value: String): String? {
if ('%' !in value) return value
val bytes = ByteArrayOutputStream(value.length)
var i = 0
while (i < value.length) {
val c = value[i]
if (c == '%') {
if (i + 3 > value.length) return null
val hi = Character.digit(value[i + 1], 16)
val lo = Character.digit(value[i + 2], 16)
if (hi < 0 || lo < 0) return null
bytes.write((hi shl 4) or lo)
i += 3
} else {
for (b in c.toString().toByteArray(Charsets.UTF_8)) bytes.write(b.toInt())
i += 1
}
}
return bytes.toByteArray().toString(Charsets.UTF_8)
}
}
@@ -0,0 +1,424 @@
package app.hapi.protocol.markdown
import org.commonmark.ext.autolink.AutolinkExtension
import org.commonmark.ext.gfm.strikethrough.StrikethroughExtension
import org.commonmark.ext.gfm.tables.TablesExtension
import org.commonmark.node.Block
import org.commonmark.node.BlockQuote
import org.commonmark.node.FencedCodeBlock
import org.commonmark.node.Heading
import org.commonmark.node.HtmlBlock
import org.commonmark.node.ListBlock
import org.commonmark.node.Node
import org.commonmark.node.ThematicBreak
import org.commonmark.parser.Parser
/**
* Source-level markdown transforms + parser configuration, ported from the web
* client so both renderers agree on how agent output is interpreted:
*
* - [repairTables] <- `web/src/lib/remark-repair-tables.ts`
* - [disableIndentedCode] <- `web/src/lib/remark-disable-indented-code.ts`
* (micromark `disable: codeIndented` == commonmark-java
* `enabledBlockTypes` without `IndentedCodeBlock`)
* - [stripCjkAutolinkArtifacts] <- `web/src/lib/remark-strip-cjk-autolink.ts`
* - [detectFilePathLinks] / [matchWholeFilePath] / [rewriteExplicitLinkTarget]
* <- `web/src/lib/remark-file-path-links.ts`
*
* Pure JVM on purpose: everything here is unit-tested in `:core:protocol`.
*/
object MarkdownTransforms {
// ── Table repair (remark-repair-tables port) ─────────────────────────────
/** Matches one GFM separator cell: optional colons around at least one dash. */
private val SEPARATOR_CELL = Regex("""^\s*:?-+:?\s*$""")
/** Code spans are blanked before counting pipes so `a|b` does not add a cell. */
private val CODE_SPAN = Regex("`+[^`]*?`+")
/** Fence opener/closer: up to 3 leading spaces, then ``` or ~~~ (3+), then rest. */
private val FENCE_LINE = Regex("""^ {0,3}(`{3,}|~{3,})(.*)$""")
private val LEADING_WHITESPACE = Regex("""^\s*""")
/**
* Count pipe-delimited cells in one table row line of raw source.
* Strips backtick code spans first so pipes inside them are not counted.
* Skips escaped pipes (`\|`) which are literal characters, not boundaries.
*/
private fun countSourceCells(line: String): Int {
val trimmed = line.trim().replace(CODE_SPAN, "\u0000")
val inner = if (trimmed.startsWith("|")) trimmed.drop(1) else trimmed
val stripped = if (inner.endsWith("|")) inner.dropLast(1) else inner
var cells = 1
var escaped = false
for (ch in stripped) {
when {
escaped -> escaped = false
ch == '\\' -> escaped = true
ch == '|' -> cells++
}
}
return cells
}
/** True if every pipe-delimited cell matches the GFM separator pattern. */
private fun isSeparatorLine(line: String): Boolean {
val trimmed = line.trim()
if (!trimmed.contains('-')) return false
val inner = if (trimmed.startsWith("|")) trimmed.drop(1) else trimmed
val stripped = if (inner.endsWith("|")) inner.dropLast(1) else inner
val cells = stripped.split('|')
return cells.isNotEmpty() && cells.all { SEPARATOR_CELL.matches(it) }
}
/** Cell count of a separator line, or null if the line is not a separator. */
private fun countSeparatorCells(line: String): Int? {
if (!isSeparatorLine(line)) return null
val trimmed = line.trim()
val inner = if (trimmed.startsWith("|")) trimmed.drop(1) else trimmed
val stripped = if (inner.endsWith("|")) inner.dropLast(1) else inner
return stripped.split('|').size
}
/**
* Pad [sepLine] to [targetCols] cells, preserving existing alignment hints.
* Returns null when the line already has enough cells or is not a separator.
*/
private fun padSeparatorLine(sepLine: String, targetCols: Int): String? {
val trimmed = sepLine.trim()
if (trimmed.isEmpty()) return null
val hasLeading = trimmed.startsWith("|")
val hasTrailing = trimmed.endsWith("|")
val inner = if (hasLeading) trimmed.drop(1) else trimmed
val stripped = if (inner.endsWith("|")) inner.dropLast(1) else inner
val cells = stripped.split('|')
if (cells.size >= targetCols) return null
if (!cells.all { SEPARATOR_CELL.matches(it) }) return null
val padded = cells + List(targetCols - cells.size) { " --- " }
val paddedInner = padded.joinToString("|")
return (if (hasLeading) "|" else "") + paddedInner + (if (hasTrailing) "|" else "")
}
/**
* Scan raw markdown for broken table separator rows (streaming-truncated GFM
* tables where the delimiter row has fewer cells than the header row) and pad
* them in place BEFORE parsing. GFM parsers degrade a mismatched-separator
* table block to a paragraph, so this must run at the source level.
*
* Fenced code blocks are tracked so table-like lines inside ``` / ~~~ fences
* are never modified (closing fence must match the opener's marker family and
* be at least as long, GFM 4.5). Leading whitespace of the separator line is
* preserved so indented tables are unaffected.
*/
fun repairTables(source: String): String {
val lines = source.split("\n").toMutableList()
var changed = false
var fenceChar: Char? = null
var fenceLength = 0
for (i in lines.indices) {
val fenceMatch = FENCE_LINE.find(lines[i])
if (fenceMatch != null) {
val marker = fenceMatch.groupValues[1]
val ch = marker[0]
val rest = fenceMatch.groupValues[2]
if (fenceChar == null) {
fenceChar = ch
fenceLength = marker.length
} else if (ch == fenceChar && marker.length >= fenceLength && rest.isBlank()) {
fenceChar = null
fenceLength = 0
}
continue
}
if (fenceChar != null) continue
if (i == 0) continue
val sep = lines[i]
if (!isSeparatorLine(sep)) continue
val hdr = lines[i - 1]
// Only repair when the header row starts with | (the common LLM form).
if (!hdr.trim().startsWith("|")) continue
val headerCols = countSourceCells(hdr)
val sepCols = countSeparatorCells(sep) ?: continue
if (sepCols >= headerCols) continue
val repaired = padSeparatorLine(sep, headerCols) ?: continue
val prefix = LEADING_WHITESPACE.find(sep)?.value ?: ""
lines[i] = prefix + repaired
changed = true
}
return if (changed) lines.joinToString("\n") else source
}
// ── Indented-code disable + parser factory ───────────────────────────────
/**
* Every commonmark-java core block type EXCEPT [org.commonmark.node.IndentedCodeBlock].
* Passing this to [Parser.Builder.enabledBlockTypes] replicates the web's
* micromark `disable: ['codeIndented']`: 4-space-indented text stays prose
* (LLM output frequently indents list continuations), fenced code still works.
*/
val BLOCK_TYPES_WITHOUT_INDENTED_CODE: Set<Class<out Block>> = setOf(
BlockQuote::class.java,
Heading::class.java,
FencedCodeBlock::class.java,
HtmlBlock::class.java,
ThematicBreak::class.java,
ListBlock::class.java,
)
/** Apply the indented-code disable to a parser builder (web-parity transform). */
fun disableIndentedCode(builder: Parser.Builder): Parser.Builder =
builder.enabledBlockTypes(BLOCK_TYPES_WITHOUT_INDENTED_CODE)
/**
* The canonical HAPI markdown parser: GFM tables + strikethrough (double
* tilde only, matching web `remark-gfm {singleTilde: false}` -- a single
* tilde is common in shell prompts) + bare-URL autolink, with indented code
* blocks disabled. Thread-safe and reusable.
*/
fun newParser(): Parser = disableIndentedCode(
Parser.builder().extensions(
listOf(
TablesExtension.create(),
StrikethroughExtension.builder().requireTwoTildes(true).build(),
AutolinkExtension.create(),
)
)
).build()
private val sharedParser: Parser by lazy { newParser() }
/** Full source pipeline: [repairTables] then parse with [newParser] config. */
fun parse(source: String): Node = sharedParser.parse(repairTables(source))
// ── CJK autolink artifact strip (remark-strip-cjk-autolink port) ─────────
/**
* CJK / fullwidth sentence-ending punctuation that should never be part of a
* URL, optionally followed by closing brackets (valid URL chars on their own,
* stripped only when they trail sentence-enders).
*/
private val TRAILING_CJK_PUNCT = Regex("(?:[\uFF0C\u3002\u3001\uFF1B\uFF1A\uFF01\uFF1F\u3000\uFF0E]+[\uFF09\u3011\u300D\u300F\u300B\u3009]*)$")
/** Result of [stripCjkAutolinkArtifacts]: clean URL + punctuation to re-emit as text. */
data class AutolinkSplit(val url: String, val trailing: String)
/**
* Auto-linkers only understand ASCII boundaries, so a URL directly followed by
* CJK punctuation (`https://x.dev/a,`) swallows the punctuation into the link.
* Splits the swallowed trailing punctuation off the URL. Apply to autolinked
* Link nodes only (link text == destination); explicit `[text](url)` links are
* left alone by the caller, mirroring the web plugin.
*/
fun stripCjkAutolinkArtifacts(url: String): AutolinkSplit {
val match = TRAILING_CJK_PUNCT.find(url) ?: return AutolinkSplit(url, "")
val punct = match.value
return AutolinkSplit(url.dropLast(punct.length), punct)
}
// ── File path link detection (remark-file-path-links port) ───────────────
/**
* Extensions that autolink to the session file viewer. Intentionally
* allowlisted (not "any dotted word") so prose like `example.org` or version
* numbers never become dead file links; TLD lookalikes (org/com/io/dev/co)
* are deliberately excluded. Mirrors `COMMON_FILE_EXTENSIONS` on the web.
*/
val COMMON_FILE_EXTENSIONS: Set<String> = setOf(
"adoc", "astro", "avif", "bat", "bmp", "c", "cfg", "cjs", "conf", "cpp", "css", "csv",
"env", "gif", "go", "gql", "gradle", "graphql", "h", "hpp", "html", "ico", "ini", "java",
"jpeg", "jpg", "js", "json", "jsx", "kt", "lock", "md", "mdx", "mjs", "mmd", "php", "png",
"prisma", "properties", "proto", "ps1", "puml", "py", "rb", "rs", "rst", "scss", "sh",
"sql", "svelte", "svg", "swift", "tex", "toml", "ts", "tsv", "tsx", "txt", "vue", "webp",
"xml", "yaml", "yml", "zsh",
)
/**
* Same alternation as the web `PATH_PATTERN`:
* 1. prefixed paths -- `C:\`/`C:/`, `./`, or `segment/` -- lazily up to a dotted
* extension (1-12 alphanumerics or `lock`), optional `:line(:col)` suffix;
* 2. bare `name.ext`, optional `:line(:col)` suffix.
*/
private val PATH_PATTERN = Regex(
"""(?:[A-Za-z]:[\\/]|\./|[A-Za-z0-9_.-]+/)[^\s`"'<>]*?\.(?:[A-Za-z0-9]{1,12}|lock)(?::\d+(?::\d+)?)?|(?:[A-Za-z0-9_.-]+\.(?:[A-Za-z0-9]{1,12}|lock))(?::\d+(?::\d+)?)?"""
)
private val LINE_SUFFIX = Regex(""":(\d+)(?::(\d+))?$""")
private val WINDOWS_ABS_PATH = Regex("""^[A-Za-z]:[\\/]""")
private val TRAILING_PUNCTUATION = setOf('.', ',', ';', ':', '!', '?')
/**
* A linkable file path found in plain text.
*
* @param range indices in the input covering the displayed path (line suffix
* included, trailing punctuation excluded)
* @param path the path with any `:line(:col)` suffix stripped
* @param line 1-based line from a `path:line` / `path:line:col` suffix
* @param column 1-based column from a `path:line:col` suffix
* @param display the exact matched text (path plus any line suffix)
*/
data class FilePathLink(
val range: IntRange,
val path: String,
val line: Int?,
val column: Int? = null,
val display: String = path,
)
/** Strip trailing prose punctuation and unbalanced closing brackets. */
private fun splitTrailingPunctuation(value: String): Pair<String, String> {
var path = value
var trailing = ""
while (path.isNotEmpty()) {
val last = path.last()
when {
last in TRAILING_PUNCTUATION -> {
trailing = last + trailing
path = path.dropLast(1)
}
last == ')' && path.count { it == '(' } <= path.count { it == ')' } -> {
trailing = last + trailing
path = path.dropLast(1)
}
last == ']' || last == '}' -> {
trailing = last + trailing
path = path.dropLast(1)
}
else -> return path to trailing
}
}
return path to trailing
}
private fun stripLineSuffix(value: String): String = LINE_SUFFIX.replace(value, "")
private fun parseLineSuffix(value: String): Pair<Int?, Int?> {
val match = LINE_SUFFIX.find(value) ?: return null to null
val line = match.groupValues[1].toIntOrNull()
val column = match.groupValues[2].takeIf { it.isNotEmpty() }?.toIntOrNull()
return line to column
}
/** True when the value (line suffix ignored) ends in an allowlisted extension. */
fun hasKnownFileExtension(value: String): Boolean {
val path = stripLineSuffix(value).lowercase()
val dot = path.lastIndexOf('.')
if (dot < 0 || dot == path.length - 1) return false
return path.substring(dot + 1) in COMMON_FILE_EXTENSIONS
}
fun isWindowsAbsolutePath(value: String): Boolean = WINDOWS_ABS_PATH.containsMatchIn(value)
/**
* Web `shouldLinkPath`: no scheme, length >= 3, not POSIX-absolute or `~/`
* (those need workspace containment, applied by the click handler), no parent
* traversal, and an allowlisted extension. Windows-absolute paths pass here
* (the web encodes them as containment-checked "candidates"); the native
* link handler owns that decision.
*/
fun shouldLinkPath(value: String): Boolean {
if (value.contains("://")) return false
val path = stripLineSuffix(value)
if (path.length < 3) return false
if (path.startsWith("/") || path.startsWith("~/")) return false
if (path.startsWith("../") || path.contains("/../")) return false
return hasKnownFileExtension(path)
}
/**
* Find linkable file paths in plain prose (the text-node pass of the web
* plugin). Skips matches directly preceded by `:`/`/`/`\`/`.` (mid-URL or
* mid-path fragments) and everything rejected by [shouldLinkPath].
*/
fun detectFilePathLinks(text: String): List<FilePathLink> {
val results = mutableListOf<FilePathLink>()
for (match in PATH_PATTERN.findAll(text)) {
val start = match.range.first
if (start > 0) {
val prev = text[start - 1]
if (prev == ':' || prev == '/' || prev == '\\' || prev == '.') continue
}
val (display, _) = splitTrailingPunctuation(match.value)
if (display.isEmpty()) continue
val path = stripLineSuffix(display)
if (!shouldLinkPath(path)) continue
val (line, column) = parseLineSuffix(display)
results += FilePathLink(
range = start until start + display.length,
path = path,
line = line,
column = column,
display = display,
)
}
return results
}
/**
* Inline-code pass: link a code span only when its ENTIRE value is a single
* linkable path (whitespace-free, pattern covers the whole value). Keeps real
* snippets (`npm run build`, `str.split()`) untouched, like the web plugin.
*/
fun matchWholeFilePath(rawValue: String): FilePathLink? {
val trimmed = rawValue.trim()
if (trimmed.isEmpty()) return null
if (trimmed.any { it.isWhitespace() }) return null
val match = PATH_PATTERN.find(trimmed) ?: return null
if (match.value != trimmed) return null
val path = stripLineSuffix(trimmed)
if (!shouldLinkPath(path)) return null
val (line, column) = parseLineSuffix(trimmed)
return FilePathLink(
range = 0 until trimmed.length,
path = path,
line = line,
column = column,
display = trimmed,
)
}
/**
* Explicit `[label](target)` rewrite (web `rewriteFileLinkNode`): returns the
* workspace file path a markdown link should open, or null when the target
* must keep its URL semantics. `#fragment` / `?query` / `:line` suffixes are
* stripped; POSIX-absolute and scheme-bearing (colon) targets are rejected;
* Windows-absolute targets require an allowlisted extension.
*/
fun rewriteExplicitLinkTarget(url: String): String? {
if (url.isEmpty()) return null
val hashIdx = url.indexOf('#')
val queryIdx = url.indexOf('?')
val cut = when {
hashIdx >= 0 && queryIdx >= 0 -> minOf(hashIdx, queryIdx)
hashIdx >= 0 -> hashIdx
queryIdx >= 0 -> queryIdx
else -> -1
}
val withoutMeta = if (cut >= 0) url.substring(0, cut) else url
val target = stripLineSuffix(withoutMeta)
if (isWindowsAbsolutePath(target)) {
return if (hasKnownFileExtension(target)) target else null
}
if (target.startsWith("/") && !target.startsWith("//")) return null
if (target.contains(':')) return null
if (!shouldLinkPath(target)) return null
return target
}
}
@@ -0,0 +1,294 @@
package app.hapi.protocol.git
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
import kotlin.test.assertTrue
class UnifiedDiffParserTest {
@Test
fun `parses a modification with two hunks and a section heading`() {
val diff = """
diff --git a/src/app.ts b/src/app.ts
index 83db48f..bf269f4 100644
--- a/src/app.ts
+++ b/src/app.ts
@@ -1,4 +1,5 @@ function main()
import fs from 'fs'
-const a = 1
+const a = 2
+const b = 3
console.log(a)
export {}
@@ -10,2 +11,2 @@
tail1
-tail2
+tail2!
""".trimIndent()
val file = UnifiedDiffParser.parse(diff).single()
assertEquals("src/app.ts", file.oldPath)
assertEquals("src/app.ts", file.newPath)
assertEquals(DiffChangeKind.MODIFY, file.changeKind)
assertEquals(false, file.isBinary)
assertEquals(2, file.hunks.size)
assertEquals(3, file.additions)
assertEquals(2, file.deletions)
val hunk = file.hunks[0]
assertEquals("function main()", hunk.sectionHeading)
assertEquals(1, hunk.oldStart)
assertEquals(4, hunk.oldCount)
assertEquals(1, hunk.newStart)
assertEquals(5, hunk.newCount)
assertEquals(
listOf(
DiffLine(DiffLineKind.CONTEXT, 1, 1, "import fs from 'fs'"),
DiffLine(DiffLineKind.REMOVE, 2, null, "const a = 1"),
DiffLine(DiffLineKind.ADD, null, 2, "const a = 2"),
DiffLine(DiffLineKind.ADD, null, 3, "const b = 3"),
DiffLine(DiffLineKind.CONTEXT, 3, 4, "console.log(a)"),
DiffLine(DiffLineKind.CONTEXT, 4, 5, "export {}"),
),
hunk.lines,
)
val second = file.hunks[1]
assertNull(second.sectionHeading)
assertEquals(
listOf(
DiffLine(DiffLineKind.CONTEXT, 10, 11, "tail1"),
DiffLine(DiffLineKind.REMOVE, 11, null, "tail2"),
DiffLine(DiffLineKind.ADD, null, 12, "tail2!"),
),
second.lines,
)
}
@Test
fun `parses a new file with a missing trailing newline`() {
val diff = """
diff --git a/notes.md b/notes.md
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/notes.md
@@ -0,0 +1,2 @@
+hello
+world
\ No newline at end of file
""".trimIndent()
val file = UnifiedDiffParser.parse(diff).single()
assertNull(file.oldPath)
assertEquals("notes.md", file.newPath)
assertEquals(DiffChangeKind.ADD, file.changeKind)
assertEquals("100644", file.newMode)
assertEquals("notes.md", file.displayPath)
val lines = file.hunks.single().lines
assertEquals(
listOf(
DiffLine(DiffLineKind.ADD, null, 1, "hello"),
DiffLine(DiffLineKind.ADD, null, 2, "world", noNewlineAtEnd = true),
),
lines,
)
}
@Test
fun `parses a deleted file`() {
val diff = """
diff --git a/old.txt b/old.txt
deleted file mode 100644
index 4b5fa63..0000000
--- a/old.txt
+++ /dev/null
@@ -1,2 +0,0 @@
-line one
-line two
""".trimIndent()
val file = UnifiedDiffParser.parse(diff).single()
assertEquals("old.txt", file.oldPath)
assertNull(file.newPath)
assertEquals(DiffChangeKind.DELETE, file.changeKind)
assertEquals("100644", file.oldMode)
assertEquals("old.txt", file.displayPath)
assertEquals(
listOf(
DiffLine(DiffLineKind.REMOVE, 1, null, "line one"),
DiffLine(DiffLineKind.REMOVE, 2, null, "line two"),
),
file.hunks.single().lines,
)
}
@Test
fun `parses a rename with an edit`() {
val diff = """
diff --git a/src/a.ts b/src/b.ts
similarity index 87%
rename from src/a.ts
rename to src/b.ts
index 1111111..2222222 100644
--- a/src/a.ts
+++ b/src/b.ts
@@ -3,3 +3,3 @@
keep
-old line
+new line
keep2
""".trimIndent()
val file = UnifiedDiffParser.parse(diff).single()
assertEquals(DiffChangeKind.RENAME, file.changeKind)
assertEquals("src/a.ts", file.oldPath)
assertEquals("src/b.ts", file.newPath)
assertEquals(87, file.similarity)
val lines = file.hunks.single().lines
assertEquals(DiffLine(DiffLineKind.CONTEXT, 3, 3, "keep"), lines[0])
assertEquals(DiffLine(DiffLineKind.REMOVE, 4, null, "old line"), lines[1])
assertEquals(DiffLine(DiffLineKind.ADD, null, 4, "new line"), lines[2])
assertEquals(DiffLine(DiffLineKind.CONTEXT, 5, 5, "keep2"), lines[3])
}
@Test
fun `parses a pure rename without hunks`() {
val diff = """
diff --git a/docs/x.md b/docs/y.md
similarity index 100%
rename from docs/x.md
rename to docs/y.md
""".trimIndent()
val file = UnifiedDiffParser.parse(diff).single()
assertEquals(DiffChangeKind.RENAME, file.changeKind)
assertEquals("docs/x.md", file.oldPath)
assertEquals("docs/y.md", file.newPath)
assertEquals(100, file.similarity)
assertTrue(file.hunks.isEmpty())
}
@Test
fun `parses a binary file marker`() {
val diff = """
diff --git a/img/logo.png b/img/logo.png
index 1234567..89abcde 100644
Binary files a/img/logo.png and b/img/logo.png differ
""".trimIndent()
val file = UnifiedDiffParser.parse(diff).single()
assertTrue(file.isBinary)
assertEquals(DiffChangeKind.MODIFY, file.changeKind)
assertEquals("img/logo.png", file.displayPath)
assertTrue(file.hunks.isEmpty())
}
@Test
fun `parses a bare unified diff without a git header`() {
// Codex `unified_diff` tool input shape. Line semantics cross-checked
// against the web's CodexDiffView parseUnifiedDiff: context lines land
// on both sides, the no-newline marker is not content.
val diff = """
--- a/lib/util.py
+++ b/lib/util.py
@@ -1,3 +1,3 @@
import os
-x = 1
+x = 2
""".trimIndent()
val file = UnifiedDiffParser.parse(diff).single()
assertEquals("lib/util.py", file.oldPath)
assertEquals("lib/util.py", file.newPath)
assertEquals(DiffChangeKind.MODIFY, file.changeKind)
// Reconstruct both sides the way the web viewer feeds DiffView.
val lines = file.hunks.single().lines
val oldText = lines.filter { it.kind != DiffLineKind.ADD }.joinToString("\n") { it.text }
val newText = lines.filter { it.kind != DiffLineKind.REMOVE }.joinToString("\n") { it.text }
assertEquals("import os\nx = 1", oldText)
assertEquals("import os\nx = 2", newText)
}
@Test
fun `parses multiple files in one diff`() {
val diff = """
diff --git a/one.txt b/one.txt
--- a/one.txt
+++ b/one.txt
@@ -1 +1 @@
-a
+b
diff --git a/two.txt b/two.txt
new file mode 100644
--- /dev/null
+++ b/two.txt
@@ -0,0 +1 @@
+hi
""".trimIndent()
val files = UnifiedDiffParser.parse(diff)
assertEquals(2, files.size)
assertEquals("one.txt", files[0].displayPath)
assertEquals(DiffChangeKind.MODIFY, files[0].changeKind)
assertEquals("two.txt", files[1].displayPath)
assertEquals(DiffChangeKind.ADD, files[1].changeKind)
}
@Test
fun `unquotes paths with spaces`() {
val diff = """
diff --git "a/sp ace.txt" "b/sp ace.txt"
--- "a/sp ace.txt"
+++ "b/sp ace.txt"
@@ -1 +1 @@
-x
+y
""".trimIndent()
val file = UnifiedDiffParser.parse(diff).single()
assertEquals("sp ace.txt", file.oldPath)
assertEquals("sp ace.txt", file.newPath)
}
@Test
fun `empty input yields no files`() {
assertTrue(UnifiedDiffParser.parse("").isEmpty())
assertTrue(UnifiedDiffParser.parse("warning: nothing to see\n").isEmpty())
}
@Test
fun `tolerates a truncated final hunk`() {
// Streaming cutoffs must keep every line seen so far.
val diff = """
diff --git a/x.txt b/x.txt
--- a/x.txt
+++ b/x.txt
@@ -1,5 +1,5 @@
ctx
-gone
""".trimIndent()
val file = UnifiedDiffParser.parse(diff).single()
assertEquals(
listOf(
DiffLine(DiffLineKind.CONTEXT, 1, 1, "ctx"),
DiffLine(DiffLineKind.REMOVE, 2, null, "gone"),
),
file.hunks.single().lines,
)
}
}
@@ -0,0 +1,153 @@
package app.hapi.protocol.markdown
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
import kotlin.test.assertTrue
class FilePathLinksTest {
// ── detectFilePathLinks ──────────────────────────────────────────────────
@Test
fun `links a unix relative path in prose`() {
val text = "see src/lib/utils.ts for details"
val links = MarkdownTransforms.detectFilePathLinks(text)
assertEquals(1, links.size)
val link = links.single()
assertEquals("src/lib/utils.ts", link.path)
assertNull(link.line)
assertEquals("src/lib/utils.ts", text.substring(link.range))
}
@Test
fun `parses line and column suffixes`() {
val links = MarkdownTransforms.detectFilePathLinks("open a/b.ts:12 and c/d.py:3:7 please")
assertEquals(2, links.size)
assertEquals("a/b.ts", links[0].path)
assertEquals(12, links[0].line)
assertNull(links[0].column)
assertEquals("a/b.ts:12", links[0].display)
assertEquals("c/d.py", links[1].path)
assertEquals(3, links[1].line)
assertEquals(7, links[1].column)
}
@Test
fun `strips trailing prose punctuation from the display path`() {
val text = "read docs/guide.md."
val link = MarkdownTransforms.detectFilePathLinks(text).single()
assertEquals("docs/guide.md", link.path)
assertEquals("docs/guide.md", text.substring(link.range))
}
@Test
fun `strips an unbalanced closing paren`() {
val link = MarkdownTransforms.detectFilePathLinks("(see src/a.ts)").single()
assertEquals("src/a.ts", link.path)
}
@Test
fun `links dot-slash paths and lockfiles`() {
val links = MarkdownTransforms.detectFilePathLinks("./local.md and yarn.lock changed")
assertEquals(listOf("./local.md", "yarn.lock"), links.map { it.path })
}
@Test
fun `links windows absolute paths`() {
val link = MarkdownTransforms.detectFilePathLinks("check C:\\proj\\app.kt now").single()
assertEquals("C:\\proj\\app.kt", link.path)
}
@Test
fun `bare names with allowlisted extensions link (web parity)`() {
// Same behavior as the web plugin: `Node.js` has an allowlisted
// extension and links; the extension allowlist (no org/com/io) is what
// keeps domains and most prose out.
val links = MarkdownTransforms.detectFilePathLinks("Node.js is great")
assertEquals(listOf("Node.js"), links.map { it.path })
}
@Test
fun `does not link paths inside urls`() {
assertTrue(MarkdownTransforms.detectFilePathLinks("https://example.com/foo.ts").isEmpty())
}
@Test
fun `does not link domains without file extensions`() {
assertTrue(MarkdownTransforms.detectFilePathLinks("visit example.org today").isEmpty())
}
@Test
fun `does not link ratios or version numbers`() {
assertTrue(MarkdownTransforms.detectFilePathLinks("a ratio of 16:9").isEmpty())
assertTrue(MarkdownTransforms.detectFilePathLinks("v1.2.3 released").isEmpty())
}
@Test
fun `does not link posix absolute or parent-traversal paths`() {
assertTrue(MarkdownTransforms.detectFilePathLinks("/abs/path/file.ts").isEmpty())
assertTrue(MarkdownTransforms.detectFilePathLinks("../up/secret.ts").isEmpty())
}
// ── matchWholeFilePath (inline code spans) ───────────────────────────────
@Test
fun `whole-value code span paths link`() {
val link = MarkdownTransforms.matchWholeFilePath("web/src/foo.tsx")
assertEquals("web/src/foo.tsx", link?.path)
}
@Test
fun `code span with line suffix links and carries the line`() {
val link = MarkdownTransforms.matchWholeFilePath("hub/src/startHub.ts:345")
assertEquals("hub/src/startHub.ts", link?.path)
assertEquals(345, link?.line)
}
@Test
fun `real code snippets never link`() {
assertNull(MarkdownTransforms.matchWholeFilePath("npm run build"))
assertNull(MarkdownTransforms.matchWholeFilePath("str.split()"))
assertNull(MarkdownTransforms.matchWholeFilePath("x.md#y"))
assertNull(MarkdownTransforms.matchWholeFilePath("Math.PI"))
assertNull(MarkdownTransforms.matchWholeFilePath(""))
}
// ── rewriteExplicitLinkTarget ────────────────────────────────────────────
@Test
fun `rewrites repo-relative link targets`() {
assertEquals("docs/guide.md", MarkdownTransforms.rewriteExplicitLinkTarget("docs/guide.md#install"))
assertEquals("src/a.ts", MarkdownTransforms.rewriteExplicitLinkTarget("src/a.ts:12"))
assertEquals("./readme.md", MarkdownTransforms.rewriteExplicitLinkTarget("./readme.md"))
assertEquals("sub/dir/img.png", MarkdownTransforms.rewriteExplicitLinkTarget("sub/dir/img.png?x=1"))
}
@Test
fun `keeps url semantics for absolute schemed and unknown targets`() {
assertNull(MarkdownTransforms.rewriteExplicitLinkTarget("/abs/a.ts"))
assertNull(MarkdownTransforms.rewriteExplicitLinkTarget("https://x.com/a.ts"))
assertNull(MarkdownTransforms.rewriteExplicitLinkTarget("mailto:a@b.c"))
assertNull(MarkdownTransforms.rewriteExplicitLinkTarget("../up.ts"))
assertNull(MarkdownTransforms.rewriteExplicitLinkTarget("/settings"))
assertNull(MarkdownTransforms.rewriteExplicitLinkTarget("#section"))
}
@Test
fun `windows absolute targets need an allowlisted extension`() {
assertEquals("C:/w/a.ts", MarkdownTransforms.rewriteExplicitLinkTarget("C:/w/a.ts"))
assertNull(MarkdownTransforms.rewriteExplicitLinkTarget("C:/w/a"))
}
}
@@ -0,0 +1,82 @@
package app.hapi.protocol.markdown
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
class HrefPolicyTest {
@Test
fun `iana safe schemes are allowed`() {
assertEquals(HrefDecision.Allowed, HrefPolicy.classify("https://example.com/a"))
assertEquals(HrefDecision.Allowed, HrefPolicy.classify("HTTPS://EXAMPLE.COM"))
assertEquals(HrefDecision.Allowed, HrefPolicy.classify("http://10.0.0.2:3006"))
assertEquals(HrefDecision.Allowed, HrefPolicy.classify("mailto:a@b.c"))
assertEquals(HrefDecision.Allowed, HrefPolicy.classify("ircs://irc.libera.chat/kotlin"))
assertEquals(HrefDecision.Allowed, HrefPolicy.classify("xmpp:user@host"))
}
@Test
fun `deny-listed schemes are blocked`() {
assertEquals(HrefDecision.Blocked, HrefPolicy.classify("javascript:alert(1)"))
assertEquals(HrefDecision.Blocked, HrefPolicy.classify("JaVaScRiPt:alert(1)"))
assertEquals(HrefDecision.Blocked, HrefPolicy.classify("data:text/html;base64,PGI+"))
assertEquals(HrefDecision.Blocked, HrefPolicy.classify("vbscript:msgbox(1)"))
assertEquals(HrefDecision.Blocked, HrefPolicy.classify("file:///etc/passwd"))
}
@Test
fun `scheme obfuscation bypasses are caught`() {
// Control characters and whitespace inside the scheme are stripped the
// way platform URL dispatch does.
assertEquals(HrefDecision.Blocked, HrefPolicy.classify("java\nscript:alert(1)"))
assertEquals(HrefDecision.Blocked, HrefPolicy.classify("\tjavascript:alert(1)"))
// Single- and double-percent-encoded separators.
assertEquals(HrefDecision.Blocked, HrefPolicy.classify("javascript%3Aalert(1)"))
assertEquals(HrefDecision.Blocked, HrefPolicy.classify("javascript%253Aalert(1)"))
assertEquals(HrefDecision.Blocked, HrefPolicy.classify("%6A%61vascript:alert(1)"))
}
@Test
fun `custom schemes require confirmation`() {
assertEquals(HrefDecision.ConfirmFirst("vscode"), HrefPolicy.classify("vscode://file/a.ts"))
assertEquals(HrefDecision.ConfirmFirst("obsidian"), HrefPolicy.classify("obsidian://open?vault=x"))
assertEquals(HrefDecision.ConfirmFirst("slack"), HrefPolicy.classify("slack://channel?id=1"))
}
@Test
fun `scheme-less targets are inert on native`() {
// No SPA router here: app routes, anchors, and protocol-relative URLs
// from the web policy all fail closed (workspace files are handled by
// rewriteExplicitLinkTarget before classify is consulted).
assertEquals(HrefDecision.Blocked, HrefPolicy.classify(""))
assertEquals(HrefDecision.Blocked, HrefPolicy.classify("/settings"))
assertEquals(HrefDecision.Blocked, HrefPolicy.classify("docs/a.md"))
assertEquals(HrefDecision.Blocked, HrefPolicy.classify("#anchor"))
assertEquals(HrefDecision.Blocked, HrefPolicy.classify("//example.com/x"))
}
@Test
fun `windows drive paths are never custom schemes`() {
assertEquals(HrefDecision.Blocked, HrefPolicy.classify("C:\\Users\\a\\b.ts"))
assertEquals(HrefDecision.Blocked, HrefPolicy.classify("c:/work/b.ts"))
}
@Test
fun `hasScheme distinguishes schemes from path colons`() {
assertTrue(HrefPolicy.hasScheme("mailto:a@b.c"))
assertTrue(HrefPolicy.hasScheme("vscode://file/a"))
assertFalse(HrefPolicy.hasScheme("docs/a.md"))
assertFalse(HrefPolicy.hasScheme("/path:12"))
assertFalse(HrefPolicy.hasScheme("#section"))
assertFalse(HrefPolicy.hasScheme(":oops"))
}
@Test
fun `normalizedScheme survives malformed escapes`() {
// Invalid escape: decoding fails, the raw value is classified as-is
// (matches the web try/catch-break loop).
assertEquals("https", HrefPolicy.normalizedScheme("https://a%ZZb"))
}
}
@@ -0,0 +1,202 @@
package app.hapi.protocol.markdown
import org.commonmark.ext.gfm.strikethrough.Strikethrough
import org.commonmark.ext.gfm.tables.TableBlock
import org.commonmark.ext.gfm.tables.TableCell
import org.commonmark.ext.gfm.tables.TableHead
import org.commonmark.node.FencedCodeBlock
import org.commonmark.node.IndentedCodeBlock
import org.commonmark.node.Link
import org.commonmark.node.Node
import org.commonmark.node.Paragraph
import org.commonmark.node.Text
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertSame
import kotlin.test.assertTrue
class MarkdownTransformsTest {
// ── repairTables (remark-repair-tables parity) ───────────────────────────
@Test
fun `pads a separator row truncated by streaming`() {
val source = "| A | B | C |\n| --- | --- |\n| 1 | 2 | 3 |"
val repaired = MarkdownTransforms.repairTables(source)
assertEquals("| A | B | C |\n| --- | --- | --- |\n| 1 | 2 | 3 |", repaired)
}
@Test
fun `preserves alignment hints in existing separator cells`() {
val source = "| A | B | C |\n|:--|--:|"
val repaired = MarkdownTransforms.repairTables(source)
assertEquals("| A | B | C |\n|:--|--:| --- |", repaired)
}
@Test
fun `repairs a separator truncated to a single cell`() {
val source = "| Name | Type | Default |\n| --- |"
val repaired = MarkdownTransforms.repairTables(source)
assertEquals("| Name | Type | Default |\n| --- | --- | --- |", repaired)
}
@Test
fun `leaves headers without a leading pipe alone`() {
val source = "A | B | C\n| --- | --- |"
assertSame(source, MarkdownTransforms.repairTables(source))
}
@Test
fun `never touches table-like lines inside fenced code`() {
val source = "```\n| A | B | C |\n| --- |\n```"
assertSame(source, MarkdownTransforms.repairTables(source))
}
@Test
fun `a shorter fence does not close a longer opener`() {
// GFM 4.5: the closer must be at least as long as the opener, so the
// inner ``` keeps the ```` fence open and the table stays untouched.
val source = "````\n```\n| A | B |\n| --- |\n````"
assertSame(source, MarkdownTransforms.repairTables(source))
}
@Test
fun `pipes inside code spans do not count as cells`() {
val source = "| `a|b` | c |\n| --- |"
assertEquals("| `a|b` | c |\n| --- | --- |", MarkdownTransforms.repairTables(source))
}
@Test
fun `escaped pipes do not count as cells`() {
val source = "| a \\| b | c |\n| --- |"
assertEquals("| a \\| b | c |\n| --- | --- |", MarkdownTransforms.repairTables(source))
}
@Test
fun `keeps indentation when replacing the separator`() {
val source = " | A | B |\n | --- |"
assertEquals(" | A | B |\n | --- | --- |", MarkdownTransforms.repairTables(source))
}
@Test
fun `returns the same instance when nothing needs repair`() {
val source = "| A | B |\n| --- | --- |\n| 1 | 2 |"
assertSame(source, MarkdownTransforms.repairTables(source))
}
// ── Parser configuration (disableIndentedCode + extensions) ──────────────
@Test
fun `indented code blocks are disabled`() {
val doc = MarkdownTransforms.parse("intro\n\n 1. indented list continuation")
assertTrue(collect(doc).none { it is IndentedCodeBlock })
}
@Test
fun `fenced code blocks still parse`() {
val doc = MarkdownTransforms.parse("```kotlin\nval x = 1\n```")
val fence = collect(doc).filterIsInstance<FencedCodeBlock>().single()
assertEquals("kotlin", fence.info)
assertEquals("val x = 1\n", fence.literal)
}
@Test
fun `double tilde strikes through but single tilde stays literal`() {
val doc = MarkdownTransforms.parse("a ~~gone~~ and user@host:~\$ stays")
val nodes = collect(doc)
assertEquals(1, nodes.count { it is Strikethrough })
val text = nodes.filterIsInstance<Text>().joinToString("") { it.literal }
assertTrue(text.contains("user@host:~\$ stays"))
}
@Test
fun `broken table parses into a full-width table after repair`() {
val doc = MarkdownTransforms.parse("| A | B | C |\n| --- |\n| 1 | 2 | 3 |")
val nodes = collect(doc)
assertEquals(1, nodes.count { it is TableBlock })
val head = nodes.filterIsInstance<TableHead>().single()
assertEquals(3, collect(head).count { it is TableCell })
}
@Test
fun `bare urls autolink`() {
val doc = MarkdownTransforms.parse("see https://example.com/docs now")
val link = collect(doc).filterIsInstance<Link>().single()
assertEquals("https://example.com/docs", link.destination)
}
@Test
fun `four-space indent inside prose stays a paragraph`() {
val doc = MarkdownTransforms.parse(" plain indented prose")
assertTrue(doc.firstChild is Paragraph)
}
// ── stripCjkAutolinkArtifacts ────────────────────────────────────────────
@Test
fun `strips trailing cjk comma from an autolinked url`() {
val split = MarkdownTransforms.stripCjkAutolinkArtifacts("https://example.com/a,")
assertEquals("https://example.com/a", split.url)
assertEquals(",", split.trailing)
}
@Test
fun `strips sentence-ender plus closing bracket`() {
val split = MarkdownTransforms.stripCjkAutolinkArtifacts("https://example.com/a。)")
assertEquals("https://example.com/a", split.url)
assertEquals("。)", split.trailing)
}
@Test
fun `plain ascii urls come back unchanged`() {
val split = MarkdownTransforms.stripCjkAutolinkArtifacts("https://example.com/path")
assertEquals("https://example.com/path", split.url)
assertEquals("", split.trailing)
}
@Test
fun `a closing bracket alone is not stripped`() {
// Brackets are valid URL characters; they only come off behind a
// sentence-ending punctuation mark, mirroring the web regex.
val split = MarkdownTransforms.stripCjkAutolinkArtifacts("https://example.com/a)")
assertEquals("https://example.com/a)", split.url)
assertEquals("", split.trailing)
}
private fun collect(root: Node): List<Node> {
val out = mutableListOf<Node>()
fun walk(node: Node) {
out += node
var child = node.firstChild
while (child != null) {
walk(child)
child = child.next
}
}
walk(root)
return out
}
}