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
+7
View File
@@ -55,6 +55,13 @@ dependencies {
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.activity.compose)
// Markdown rendering (B-M2d1). commonmark comes through :core:protocol's
// `api` too; declared here because ui/markdown walks the AST types directly.
implementation(libs.commonmark)
implementation(libs.commonmark.ext.gfm.tables)
implementation(libs.commonmark.ext.gfm.strikethrough)
implementation(libs.highlights)
implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.compose.ui)
implementation(libs.androidx.compose.material3)
@@ -0,0 +1,226 @@
package app.hapi.companion.ui.components
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.IntrinsicSize
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import app.hapi.companion.R
import app.hapi.companion.ui.theme.HapiExtendedColors
import app.hapi.companion.ui.theme.hapi
import app.hapi.protocol.git.DiffChangeKind
import app.hapi.protocol.git.DiffFile
import app.hapi.protocol.git.DiffLineKind
/**
* Renders one parsed [DiffFile] (`app.hapi.protocol.git.UnifiedDiffParser`):
* header with path + add/remove badges, hunk headers, +/- tinted rows with
* dual line-number gutters, horizontal scroll. Starts compact ([compact]) and
* expands in place beyond [compactLineLimit] rows.
*/
@Composable
fun DiffView(
file: DiffFile,
modifier: Modifier = Modifier,
compact: Boolean = true,
compactLineLimit: Int = 12,
) {
val colors = MaterialTheme.hapi
var expanded by rememberSaveable(file.displayPath, file.hunks.size) { mutableStateOf(!compact) }
Column(
modifier = modifier
.clip(RoundedCornerShape(12.dp))
.background(colors.codeBackground),
) {
DiffHeader(file, colors)
if (file.isBinary) {
Text(
text = stringResource(R.string.diff_binary_file),
fontSize = 12.sp,
color = colors.hint,
modifier = Modifier.padding(horizontal = 12.dp, vertical = 10.dp),
)
return@Column
}
if (file.hunks.isEmpty()) return@Column
val rows = remember(file, colors) { buildDiffRows(file, colors) }
val visible = if (expanded) rows else rows.take(compactLineLimit)
val hidden = rows.size - visible.size
Column(
modifier = Modifier
.horizontalScroll(rememberScrollState())
.width(IntrinsicSize.Max)
.padding(vertical = 4.dp),
) {
for (row in visible) {
Row(
modifier = Modifier
.fillMaxWidth()
.background(row.background),
) {
Text(
text = row.text,
fontFamily = FontFamily.Monospace,
fontSize = 12.sp,
lineHeight = 18.sp,
softWrap = false,
maxLines = 1,
modifier = Modifier.padding(horizontal = 10.dp),
)
}
}
}
if (hidden > 0 || (expanded && rows.size > compactLineLimit)) {
Text(
text = if (hidden > 0) {
stringResource(R.string.diff_show_more, hidden)
} else {
stringResource(R.string.diff_collapse)
},
fontSize = 12.sp,
fontWeight = FontWeight.Medium,
color = MaterialTheme.colorScheme.primary,
modifier = Modifier
.fillMaxWidth()
.clickable { expanded = !expanded }
.padding(horizontal = 12.dp, vertical = 8.dp),
)
}
}
}
@Composable
private fun DiffHeader(file: DiffFile, colors: HapiExtendedColors) {
Row(
modifier = Modifier
.fillMaxWidth()
.background(colors.codeHeaderBackground)
.padding(horizontal = 12.dp, vertical = 6.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(6.dp),
) {
val title = when {
file.changeKind == DiffChangeKind.RENAME || file.changeKind == DiffChangeKind.COPY ->
"${file.oldPath ?: "?"} → ${file.newPath ?: "?"}"
else -> file.displayPath
}
Text(
text = title,
fontFamily = FontFamily.Monospace,
fontSize = 11.sp,
letterSpacing = 0.5.sp,
color = colors.codeHeaderForeground,
maxLines = 1,
overflow = TextOverflow.MiddleEllipsis,
modifier = Modifier.weight(1f),
)
val kindLabel = when (file.changeKind) {
DiffChangeKind.ADD -> "new"
DiffChangeKind.DELETE -> "deleted"
DiffChangeKind.RENAME -> "renamed"
DiffChangeKind.COPY -> "copied"
DiffChangeKind.MODIFY -> null
}
if (kindLabel != null) {
Text(
text = kindLabel,
fontSize = 10.sp,
color = colors.hint,
)
}
DiffStatBadge(added = true, value = file.additions, colors = colors)
DiffStatBadge(added = false, value = file.deletions, colors = colors)
}
}
@Composable
private fun DiffStatBadge(added: Boolean, value: Int, colors: HapiExtendedColors) {
Text(
text = (if (added) "+" else "-") + value,
fontSize = 10.sp,
fontWeight = FontWeight.Medium,
color = if (added) colors.diffAddText else colors.diffRemoveText,
modifier = Modifier
.clip(RoundedCornerShape(50))
.background(if (added) colors.diffAddBackground else colors.diffRemoveBackground)
.padding(horizontal = 7.dp, vertical = 1.dp),
)
}
private class DiffRow(val text: AnnotatedString, val background: Color)
private fun buildDiffRows(file: DiffFile, colors: HapiExtendedColors): List<DiffRow> {
val maxOld = file.hunks.maxOf { hunk -> hunk.oldStart + hunk.oldCount }
val maxNew = file.hunks.maxOf { hunk -> hunk.newStart + hunk.newCount }
val oldWidth = maxOf(maxOld.toString().length, 2)
val newWidth = maxOf(maxNew.toString().length, 2)
val gutterStyle = SpanStyle(color = colors.hint.copy(alpha = 0.8f))
val rows = mutableListOf<DiffRow>()
for (hunk in file.hunks) {
rows += DiffRow(
text = buildAnnotatedString {
withStyle(SpanStyle(color = colors.hint)) { append(hunk.header) }
},
background = colors.diffHunkBackground,
)
for (line in hunk.lines) {
val (marker, background, textColor) = when (line.kind) {
DiffLineKind.ADD -> Triple("+", colors.diffAddBackground, colors.diffAddText)
DiffLineKind.REMOVE -> Triple("-", colors.diffRemoveBackground, colors.diffRemoveText)
DiffLineKind.CONTEXT -> Triple(" ", Color.Transparent, colors.inlineCodeForeground)
}
rows += DiffRow(
text = buildAnnotatedString {
withStyle(gutterStyle) {
append((line.oldLineNumber?.toString() ?: "").padStart(oldWidth))
append(' ')
append((line.newLineNumber?.toString() ?: "").padStart(newWidth))
}
withStyle(SpanStyle(color = textColor)) {
append(" ")
append(marker)
append(' ')
append(line.text)
}
},
background = background,
)
}
}
return rows
}
@@ -0,0 +1,249 @@
package app.hapi.companion.ui.markdown
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.produceState
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import app.hapi.companion.R
import app.hapi.companion.ui.theme.HapiExtendedColors
import app.hapi.companion.ui.theme.hapi
import dev.snipme.highlights.Highlights
import dev.snipme.highlights.model.BoldHighlight
import dev.snipme.highlights.model.CodeHighlight
import dev.snipme.highlights.model.ColorHighlight
import dev.snipme.highlights.model.SyntaxLanguage
import dev.snipme.highlights.model.SyntaxThemes
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.withContext
/** Above this many lines highlighting is skipped and the block renders plain. */
private const val MAX_HIGHLIGHT_LINES = 400
private const val HIGHLIGHT_CACHE_SIZE = 200
private data class HighlightKey(
val codeHash: Int,
val codeLength: Int,
val language: String?,
val dark: Boolean,
)
/** LRU (access-order) cache of computed highlight spans, shared app-wide. */
private object HighlightCache {
private val map = object : LinkedHashMap<HighlightKey, List<CodeHighlight>>(64, 0.75f, true) {
override fun removeEldestEntry(
eldest: MutableMap.MutableEntry<HighlightKey, List<CodeHighlight>>,
): Boolean = size > HIGHLIGHT_CACHE_SIZE
}
@Synchronized
fun get(key: HighlightKey): List<CodeHighlight>? = map[key]
@Synchronized
fun put(key: HighlightKey, value: List<CodeHighlight>) {
map[key] = value
}
}
/**
* Fenced-code surface: language chip + copy button header, horizontally
* scrolling monospaced body with `highlights`-based syntax coloring computed
* off the main thread ([produceState] + LRU cache). Long blocks (>
* [MAX_HIGHLIGHT_LINES] lines) and unknown languages stay plain.
*/
@Composable
fun CodeBlock(code: String, language: String?, modifier: Modifier = Modifier) {
val colors = MaterialTheme.hapi
Column(
modifier = modifier
.clip(RoundedCornerShape(12.dp))
.background(colors.codeBackground),
) {
Row(
modifier = Modifier
.fillMaxWidth()
.background(colors.codeHeaderBackground)
.padding(start = 12.dp, end = 6.dp, top = 4.dp, bottom = 4.dp),
verticalAlignment = Alignment.CenterVertically,
) {
Text(
text = (language ?: stringResource(R.string.code_plain_fallback)).uppercase(),
fontFamily = FontFamily.Monospace,
fontSize = 11.sp,
letterSpacing = 0.8.sp,
color = colors.codeHeaderForeground,
maxLines = 1,
modifier = Modifier.weight(1f, fill = false),
)
Spacer(Modifier.weight(1f))
CopyButton(code, colors)
}
val highlighted = rememberHighlightedCode(code, language, colors.isDark)
Box(
modifier = Modifier
.fillMaxWidth()
.horizontalScroll(rememberScrollState()),
) {
Text(
text = highlighted,
fontFamily = FontFamily.Monospace,
fontSize = 13.sp,
lineHeight = 19.sp,
softWrap = false,
modifier = Modifier.padding(horizontal = 12.dp, vertical = 10.dp),
)
}
}
}
@Composable
private fun CopyButton(code: String, colors: HapiExtendedColors) {
// LocalClipboard (the suspend replacement) buys nothing for a plain text
// copy and would force a scope launch in the click handler; the deprecated
// sync API is the deliberate choice here.
@Suppress("DEPRECATION")
val clipboard = LocalClipboardManager.current
var copied by remember { mutableStateOf(false) }
LaunchedEffect(copied) {
if (copied) {
delay(1600)
copied = false
}
}
Text(
text = stringResource(if (copied) R.string.code_copied else R.string.code_copy),
fontSize = 11.sp,
fontWeight = FontWeight.Medium,
color = if (copied) MaterialTheme.colorScheme.primary else colors.codeHeaderForeground,
modifier = Modifier
.clip(RoundedCornerShape(6.dp))
.clickable {
clipboard.setText(AnnotatedString(code))
copied = true
}
.padding(horizontal = 8.dp, vertical = 4.dp),
)
}
@Composable
private fun rememberHighlightedCode(code: String, language: String?, dark: Boolean): AnnotatedString {
val eligible = language != null &&
code.isNotBlank() &&
countLines(code) <= MAX_HIGHLIGHT_LINES &&
resolveLanguage(language) != SyntaxLanguage.DEFAULT
val key = HighlightKey(code.hashCode(), code.length, language?.lowercase(), dark)
val highlighted by produceState(
initialValue = HighlightCache.get(key)?.let { applyHighlights(code, it) } ?: AnnotatedString(code),
key1 = key,
) {
// Producer restarts on key change: reset synchronously (cache or plain)
// so a recycled composable never shows spans from the previous code.
val cached = HighlightCache.get(key)
value = cached?.let { applyHighlights(code, it) } ?: AnnotatedString(code)
if (cached == null && eligible) {
val spans = withContext(Dispatchers.Default) {
computeHighlights(code, language!!, dark)
}
HighlightCache.put(key, spans)
value = applyHighlights(code, spans)
}
}
return highlighted
}
private fun countLines(code: String): Int {
var lines = 1
for (ch in code) if (ch == '\n') lines += 1
return lines
}
private val LANGUAGE_ALIASES = mapOf(
"js" to "javascript",
"jsx" to "javascript",
"mjs" to "javascript",
"cjs" to "javascript",
"ts" to "typescript",
"tsx" to "typescript",
"py" to "python",
"rb" to "ruby",
"kts" to "kotlin",
"sh" to "shell",
"bash" to "shell",
"zsh" to "shell",
"shellsession" to "shell",
"c++" to "cpp",
"cs" to "csharp",
"golang" to "go",
)
private fun resolveLanguage(raw: String?): SyntaxLanguage {
if (raw == null) return SyntaxLanguage.DEFAULT
val name = LANGUAGE_ALIASES[raw.lowercase()] ?: raw.lowercase()
return SyntaxLanguage.values().firstOrNull { it.name.equals(name, ignoreCase = true) }
?: SyntaxLanguage.DEFAULT
}
private fun computeHighlights(code: String, language: String, dark: Boolean): List<CodeHighlight> =
try {
Highlights.Builder()
.code(code)
.language(resolveLanguage(language))
.theme(SyntaxThemes.darcula(darkMode = dark))
.build()
.getHighlights()
} catch (_: Exception) {
// Highlighting is cosmetic; malformed input must never take the UI down.
emptyList()
}
private fun applyHighlights(code: String, highlights: List<CodeHighlight>): AnnotatedString {
if (highlights.isEmpty()) return AnnotatedString(code)
return buildAnnotatedString {
append(code)
for (highlight in highlights) {
val start = highlight.location.start.coerceIn(0, code.length)
val end = highlight.location.end.coerceIn(0, code.length)
if (end <= start) continue
when (highlight) {
is ColorHighlight -> addStyle(
SpanStyle(color = Color(0xFF000000.toInt() or highlight.rgb)),
start,
end,
)
is BoldHighlight -> addStyle(SpanStyle(fontWeight = FontWeight.Bold), start, end)
}
}
}
}
@@ -0,0 +1,592 @@
package app.hapi.companion.ui.markdown
import androidx.compose.foundation.background
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.IntrinsicSize
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.remember
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.layout.Measurable
import androidx.compose.ui.layout.Placeable
import androidx.compose.ui.layout.layoutId
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.LinkAnnotation
import androidx.compose.ui.text.LinkInteractionListener
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextLinkStyles
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.text.withLink
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.Constraints
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.em
import androidx.compose.ui.unit.sp
import app.hapi.companion.ui.theme.HapiExtendedColors
import app.hapi.companion.ui.theme.hapi
import app.hapi.protocol.markdown.HrefDecision
import app.hapi.protocol.markdown.HrefPolicy
import app.hapi.protocol.markdown.MarkdownTransforms
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.ext.gfm.tables.TableRow
import org.commonmark.node.BlockQuote
import org.commonmark.node.BulletList
import org.commonmark.node.Code
import org.commonmark.node.Emphasis
import org.commonmark.node.FencedCodeBlock
import org.commonmark.node.HardLineBreak
import org.commonmark.node.Heading
import org.commonmark.node.HtmlBlock
import org.commonmark.node.HtmlInline
import org.commonmark.node.Image
import org.commonmark.node.IndentedCodeBlock
import org.commonmark.node.Link
import org.commonmark.node.ListItem
import org.commonmark.node.Node
import org.commonmark.node.OrderedList
import org.commonmark.node.Paragraph
import org.commonmark.node.SoftLineBreak
import org.commonmark.node.StrongEmphasis
import org.commonmark.node.Text as MdText
import org.commonmark.node.ThematicBreak
// ── Link handling ────────────────────────────────────────────────────────────
/**
* Receives clicks on markdown links. Provided by the chat screen (M2d2 wires
* file taps to the session file viewer and URLs to a confirm-aware opener).
*/
interface MarkdownLinkHandler {
/** A workspace file citation (`src/a.ts`, `hub/src/x.ts:345`, ...). */
fun onFilePath(path: String, line: Int?)
/**
* A URL whose [HrefPolicy] decision is [HrefDecision.Allowed] or
* [HrefDecision.ConfirmFirst]; blocked destinations never reach here --
* they render as inert text.
*/
fun onUrl(url: String, decision: HrefDecision)
}
private object NoOpMarkdownLinkHandler : MarkdownLinkHandler {
override fun onFilePath(path: String, line: Int?) = Unit
override fun onUrl(url: String, decision: HrefDecision) = Unit
}
val LocalMarkdownLinkHandler = staticCompositionLocalOf<MarkdownLinkHandler> { NoOpMarkdownLinkHandler }
// ── Entry point ──────────────────────────────────────────────────────────────
/**
* Renders chat markdown with the shared HAPI pipeline: source transforms and
* parser configuration come from `:core:protocol` ([MarkdownTransforms.parse]:
* table repair, GFM tables/strikethrough/autolink, indented code disabled),
* then this walker maps the AST to Compose. Unknown fences (mermaid, math)
* degrade to plain [CodeBlock]s per the v1 plan; html blocks fall back to
* monospace text.
*/
@Composable
fun Markdown(text: String, modifier: Modifier = Modifier) {
val parsed = remember(text) { prepareDocument(text) }
val baseStyle = MaterialTheme.typography.bodyLarge.copy(fontSize = 15.sp, lineHeight = 22.sp)
CompositionLocalProvider(LocalTextStyle provides baseStyle) {
Column(modifier = modifier, verticalArrangement = Arrangement.spacedBy(10.dp)) {
MarkdownBlockChildren(parsed.document, parsed)
}
}
}
/** Parsed AST plus the task-list markers stripped out of it (identity-keyed). */
private class ParsedMarkdown(val document: Node, val taskMarkers: Map<Node, Boolean>)
private val TASK_MARKER = Regex("""^\[([ xX])\]\s+""")
/**
* Parse + normalize GFM task-list items (`- [x] done`): the textual marker is
* removed from the first text node and remembered per list item, so the walker
* can draw a checkbox prefix (the task extension jar is not needed for this).
*/
private fun prepareDocument(text: String): ParsedMarkdown {
val document = MarkdownTransforms.parse(text)
val markers = HashMap<Node, Boolean>()
var node: Node? = document
val stack = ArrayDeque<Node>()
while (node != null) {
if (node is ListItem) {
val firstText = (node.firstChild as? Paragraph)?.firstChild as? MdText
val literal = firstText?.literal
val match = literal?.let { TASK_MARKER.find(it) }
if (firstText != null && match != null) {
markers[node] = match.groupValues[1] != " "
firstText.literal = literal.removeRange(match.range)
}
}
var child = node.firstChild
while (child != null) {
stack.addLast(child)
child = child.next
}
node = stack.removeLastOrNull()
}
return ParsedMarkdown(document, markers)
}
// ── Block rendering ──────────────────────────────────────────────────────────
@Composable
private fun MarkdownBlockChildren(parent: Node, parsed: ParsedMarkdown) {
var child = parent.firstChild
while (child != null) {
MarkdownBlock(child, parsed)
child = child.next
}
}
@Composable
private fun MarkdownBlock(node: Node, parsed: ParsedMarkdown) {
when (node) {
is Paragraph -> MarkdownInlineText(node)
is Heading -> MarkdownInlineText(node, style = headingStyle(node.level))
is BlockQuote -> MarkdownBlockQuote(node, parsed)
is BulletList -> MarkdownList(node, ordered = false, parsed = parsed)
is OrderedList -> MarkdownList(node, ordered = true, parsed = parsed)
is FencedCodeBlock -> CodeBlock(
code = node.literal.orEmpty().trimEnd('\n'),
language = node.info?.trim()?.takeWhile { !it.isWhitespace() }?.ifEmpty { null },
)
is IndentedCodeBlock -> CodeBlock(code = node.literal.orEmpty().trimEnd('\n'), language = null)
is TableBlock -> MarkdownTable(node)
is ThematicBreak -> HorizontalDivider(color = MaterialTheme.hapi.divider)
is HtmlBlock -> HtmlBlockFallback(node.literal.orEmpty())
else -> if (node.firstChild != null) MarkdownBlockChildren(node, parsed)
}
}
@Composable
private fun headingStyle(level: Int): TextStyle {
// Chat headings are compact (web: 1.05rem..0.88rem semibold).
val base = LocalTextStyle.current
val size = when (level) {
1 -> 18.sp
2 -> 17.sp
3 -> 16.sp
else -> 15.sp
}
return base.copy(fontSize = size, lineHeight = size * 1.4, fontWeight = FontWeight.SemiBold)
}
@Composable
private fun MarkdownBlockQuote(node: BlockQuote, parsed: ParsedMarkdown) {
val colors = MaterialTheme.hapi
Row(
modifier = Modifier
.height(IntrinsicSize.Min)
.clip(RoundedCornerShape(topEnd = 12.dp, bottomEnd = 12.dp))
.background(colors.blockquoteBackground),
) {
Box(
Modifier
.width(3.dp)
.fillMaxHeight()
.background(colors.blockquoteBar),
)
CompositionLocalProvider(LocalContentColor provides colors.blockquoteForeground) {
Column(
modifier = Modifier.padding(horizontal = 12.dp, vertical = 8.dp),
verticalArrangement = Arrangement.spacedBy(8.dp),
) {
MarkdownBlockChildren(node, parsed)
}
}
}
}
@Composable
private fun MarkdownList(list: Node, ordered: Boolean, parsed: ParsedMarkdown) {
Column(verticalArrangement = Arrangement.spacedBy(4.dp)) {
var index = (list as? OrderedList)?.markerStartNumber ?: 1
var item = list.firstChild
while (item != null) {
if (item is ListItem) {
val task = parsed.taskMarkers[item]
val marker = when {
task == true -> "☑" // checked box
task == false -> "☐" // empty box
ordered -> "$index."
else -> "•" // bullet
}
MarkdownListItem(item, marker, parsed)
index += 1
}
item = item.next
}
}
}
@Composable
private fun MarkdownListItem(item: ListItem, marker: String, parsed: ParsedMarkdown) {
Row {
Text(
text = marker,
style = LocalTextStyle.current,
color = MaterialTheme.hapi.hint,
modifier = Modifier
.widthIn(min = 22.dp)
.padding(end = 4.dp),
)
Column(verticalArrangement = Arrangement.spacedBy(4.dp)) {
MarkdownBlockChildren(item, parsed)
}
}
}
@Composable
private fun HtmlBlockFallback(literal: String) {
Text(
text = literal.trimEnd('\n'),
style = LocalTextStyle.current.copy(
fontFamily = FontFamily.Monospace,
fontSize = 12.sp,
lineHeight = 17.sp,
),
color = MaterialTheme.hapi.hint,
)
}
// ── Table rendering ──────────────────────────────────────────────────────────
private class TableRowModel(val isHeader: Boolean, val cells: List<TableCell>)
@Composable
private fun MarkdownTable(table: TableBlock) {
val colors = MaterialTheme.hapi
val rows = remember(table) { collectTableRows(table) }
if (rows.isEmpty()) return
val columnCount = rows.maxOf { it.cells.size }
Box(
modifier = Modifier
.clip(RoundedCornerShape(12.dp))
.background(colors.tableBackground)
.horizontalScroll(rememberScrollState()),
) {
TableGrid(rows, columnCount, colors)
}
}
private fun collectTableRows(table: TableBlock): List<TableRowModel> {
val rows = mutableListOf<TableRowModel>()
var section = table.firstChild
while (section != null) {
val isHeader = section is TableHead
var row = section.firstChild
while (row != null) {
if (row is TableRow) {
val cells = mutableListOf<TableCell>()
var cell = row.firstChild
while (cell != null) {
if (cell is TableCell) cells.add(cell)
cell = cell.next
}
rows.add(TableRowModel(isHeader, cells))
}
row = row.next
}
section = section.next
}
return rows
}
/**
* Content-sized grid: column widths come from the widest cell (capped so long
* prose wraps), row backgrounds/dividers are dedicated measurables sized after
* the cells, placed behind them. The whole grid lives in a horizontal scroller.
*/
@Composable
private fun TableGrid(rows: List<TableRowModel>, columnCount: Int, colors: HapiExtendedColors) {
val cellStyle = LocalTextStyle.current.copy(fontSize = 13.sp, lineHeight = 19.sp)
Layout(
content = {
rows.forEachIndexed { r, row ->
Box(
Modifier
.layoutId("bg:$r")
.background(if (row.isHeader) colors.tableHeaderBackground else colors.tableBackground),
)
row.cells.forEachIndexed { c, cell ->
val alignment = cell.alignment
Box(
modifier = Modifier
.layoutId("cell:$r:$c")
.padding(horizontal = 10.dp, vertical = 6.dp),
contentAlignment = when (alignment) {
TableCell.Alignment.CENTER -> Alignment.TopCenter
TableCell.Alignment.RIGHT -> Alignment.TopEnd
else -> Alignment.TopStart
},
) {
MarkdownInlineText(
cell,
style = if (row.isHeader) cellStyle.copy(fontWeight = FontWeight.SemiBold) else cellStyle,
textAlign = when (alignment) {
TableCell.Alignment.CENTER -> TextAlign.Center
TableCell.Alignment.RIGHT -> TextAlign.End
else -> TextAlign.Start
},
)
}
}
}
},
) { measurables, _ ->
val cellCap = 320.dp.roundToPx()
val cells = Array(rows.size) { arrayOfNulls<Measurable>(columnCount) }
val backgrounds = arrayOfNulls<Measurable>(rows.size)
for (measurable in measurables) {
val id = measurable.layoutId as String
val parts = id.split(':')
when (parts[0]) {
"bg" -> backgrounds[parts[1].toInt()] = measurable
"cell" -> cells[parts[1].toInt()][parts[2].toInt()] = measurable
}
}
val columnWidths = IntArray(columnCount)
for (r in rows.indices) {
for (c in 0 until columnCount) {
val cell = cells[r][c] ?: continue
val intrinsic = cell.maxIntrinsicWidth(Constraints.Infinity)
columnWidths[c] = maxOf(columnWidths[c], minOf(intrinsic, cellCap))
}
}
val tableWidth = columnWidths.sum()
val placeables = Array(rows.size) { arrayOfNulls<Placeable>(columnCount) }
val rowHeights = IntArray(rows.size)
for (r in rows.indices) {
for (c in 0 until columnCount) {
val cell = cells[r][c] ?: continue
val placeable = cell.measure(
Constraints(minWidth = columnWidths[c], maxWidth = columnWidths[c]),
)
placeables[r][c] = placeable
rowHeights[r] = maxOf(rowHeights[r], placeable.height)
}
}
val backgroundPlaceables = Array(rows.size) { r ->
backgrounds[r]?.measure(Constraints.fixed(tableWidth, rowHeights[r]))
}
val tableHeight = rowHeights.sum()
layout(tableWidth, tableHeight) {
var y = 0
for (r in rows.indices) {
backgroundPlaceables[r]?.place(0, y)
var x = 0
for (c in 0 until columnCount) {
placeables[r][c]?.place(x, y)
x += columnWidths[c]
}
y += rowHeights[r]
}
}
}
}
// ── Inline rendering ─────────────────────────────────────────────────────────
@Composable
private fun MarkdownInlineText(
parent: Node,
style: TextStyle = LocalTextStyle.current,
textAlign: TextAlign? = null,
) {
val handler = LocalMarkdownLinkHandler.current
val colors = MaterialTheme.hapi
val annotated = remember(parent, colors, handler) {
buildAnnotatedString {
appendInlineChildren(parent, InlineContext(colors, handler), insideLink = false)
}
}
Text(
text = annotated,
style = if (textAlign != null) style.copy(textAlign = textAlign) else style,
)
}
private class InlineContext(val colors: HapiExtendedColors, val handler: MarkdownLinkHandler) {
val linkStyles = TextLinkStyles(
style = SpanStyle(
color = colors.link,
fontWeight = FontWeight.Medium,
textDecoration = TextDecoration.Underline,
),
)
val codeSpanStyle = SpanStyle(
fontFamily = FontFamily.Monospace,
fontSize = 0.9.em,
background = colors.inlineCodeBackground,
color = colors.inlineCodeForeground,
)
}
private fun AnnotatedString.Builder.appendInlineChildren(
parent: Node,
ctx: InlineContext,
insideLink: Boolean,
) {
var node = parent.firstChild
while (node != null) {
appendInlineNode(node, ctx, insideLink)
node = node.next
}
}
private fun AnnotatedString.Builder.appendInlineNode(node: Node, ctx: InlineContext, insideLink: Boolean) {
when (node) {
is MdText -> {
val literal = node.literal.orEmpty()
if (insideLink) append(literal) else appendTextWithFilePaths(literal, ctx)
}
// Soft breaks collapse to spaces on the assistant surface (web parity;
// user prompts get a breaks-preserving variant later).
is SoftLineBreak -> append(' ')
is HardLineBreak -> append('\n')
is Emphasis -> withStyle(SpanStyle(fontStyle = FontStyle.Italic)) {
appendInlineChildren(node, ctx, insideLink)
}
is StrongEmphasis -> withStyle(SpanStyle(fontWeight = FontWeight.SemiBold)) {
appendInlineChildren(node, ctx, insideLink)
}
is Strikethrough -> withStyle(SpanStyle(textDecoration = TextDecoration.LineThrough)) {
appendInlineChildren(node, ctx, insideLink)
}
is Code -> appendInlineCode(node.literal.orEmpty(), ctx, insideLink)
is Link -> if (insideLink) appendInlineChildren(node, ctx, true) else appendLink(node, ctx)
is Image -> appendImageFallback(node, ctx)
is HtmlInline -> append(node.literal.orEmpty())
else -> appendInlineChildren(node, ctx, insideLink)
}
}
/** Plain prose: autolink workspace file citations found by the shared detector. */
private fun AnnotatedString.Builder.appendTextWithFilePaths(literal: String, ctx: InlineContext) {
val links = MarkdownTransforms.detectFilePathLinks(literal)
if (links.isEmpty()) {
append(literal)
return
}
var cursor = 0
for (link in links) {
if (link.range.first > cursor) append(literal.substring(cursor, link.range.first))
appendFilePathLink(link.display, link.path, link.line, ctx, monospace = false)
cursor = link.range.last + 1
}
if (cursor < literal.length) append(literal.substring(cursor))
}
private fun AnnotatedString.Builder.appendFilePathLink(
display: String,
path: String,
line: Int?,
ctx: InlineContext,
monospace: Boolean,
) {
val listener = LinkInteractionListener { ctx.handler.onFilePath(path, line) }
withLink(LinkAnnotation.Clickable("hapi-file:$path", ctx.linkStyles, listener)) {
if (monospace) {
withStyle(ctx.codeSpanStyle.copy(color = ctx.colors.link)) { append(display) }
} else {
append(display)
}
}
}
/** Inline code: whole-value file paths become links, everything else is a code span. */
private fun AnnotatedString.Builder.appendInlineCode(literal: String, ctx: InlineContext, insideLink: Boolean) {
val whole = if (insideLink) null else MarkdownTransforms.matchWholeFilePath(literal)
if (whole != null) {
appendFilePathLink(whole.display, whole.path, whole.line, ctx, monospace = true)
} else {
withStyle(ctx.codeSpanStyle) { append(literal) }
}
}
private fun AnnotatedString.Builder.appendLink(node: Link, ctx: InlineContext) {
val destination = node.destination.orEmpty()
// Autolinked URLs (text == destination) may have swallowed trailing CJK
// punctuation; split it back out as plain text (web parity).
val onlyChild = node.firstChild
val isAutolink = onlyChild is MdText && onlyChild.next == null && onlyChild.literal == destination
if (isAutolink) {
val split = MarkdownTransforms.stripCjkAutolinkArtifacts(destination)
appendClassifiedLink(split.url, ctx) { append(split.url) }
if (split.trailing.isNotEmpty()) append(split.trailing)
return
}
// Explicit [label](relative/file.ext) → session file viewer.
val filePath = MarkdownTransforms.rewriteExplicitLinkTarget(destination)
if (filePath != null) {
val listener = LinkInteractionListener { ctx.handler.onFilePath(filePath, null) }
withLink(LinkAnnotation.Clickable("hapi-file:$filePath", ctx.linkStyles, listener)) {
appendInlineChildren(node, ctx, insideLink = true)
}
return
}
appendClassifiedLink(destination, ctx) { appendInlineChildren(node, ctx, insideLink = true) }
}
/** Apply [HrefPolicy]: blocked → inert hint text; otherwise clickable via the handler. */
private fun AnnotatedString.Builder.appendClassifiedLink(
url: String,
ctx: InlineContext,
content: AnnotatedString.Builder.() -> Unit,
) {
when (val decision = HrefPolicy.classify(url)) {
is HrefDecision.Blocked -> withStyle(SpanStyle(color = ctx.colors.hint)) { content() }
else -> {
val listener = LinkInteractionListener { ctx.handler.onUrl(url, decision) }
withLink(LinkAnnotation.Clickable("url:$url", ctx.linkStyles, listener)) { content() }
}
}
}
/** No inline image loading in the markdown pass (M2 wires Coil): alt text as a link. */
private fun AnnotatedString.Builder.appendImageFallback(node: Image, ctx: InlineContext) {
val destination = node.destination.orEmpty()
appendClassifiedLink(destination, ctx) {
if (node.firstChild != null) appendInlineChildren(node, ctx, insideLink = true) else append(destination)
}
}
@@ -0,0 +1,144 @@
package app.hapi.companion.ui.markdown
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.compose.ui.tooling.preview.PreviewParameterProvider
import androidx.compose.ui.unit.dp
import app.hapi.companion.ui.components.DiffView
import app.hapi.companion.ui.theme.HapiTheme
import app.hapi.protocol.git.UnifiedDiffParser
/**
* Kitchen-sink markdown exercising every renderer path (headings, emphasis,
* strikethrough, inline code + file citations, links incl. blocked/custom
* schemes, CJK autolink artifacts, lists + task lists, blockquote, tables --
* one streaming-truncated, fenced code, mermaid degradation, html fallback,
* thematic break). Compile-checked previews double as visual regressions.
*/
class MarkdownPreviewParameterProvider : PreviewParameterProvider<String> {
override val values: Sequence<String> = sequenceOf(KITCHEN_SINK_MARKDOWN)
}
// A ~ in a raw string: kept literal; ${'$'} escapes are not needed below.
val KITCHEN_SINK_MARKDOWN: String = """
# Release notes
Shipped the **markdown pipeline** with *italics*, ~~strikeouts~~, and `inline code`.
Edit `web/src/lib/remark-repair-tables.ts` or jump to hub/src/startHub.ts:345 directly.
Links: [docs](https://example.com/docs), [guide](docs/guide.md#install),
[blocked](/settings), vscode://file/readme.md, and https://example.com/a,中文标点。
## Checklist
- [x] port transforms
- [ ] wire chat surface
- plain bullet
1. first
2. second with `a/b.ts:12`
> Quoted wisdom with **bold** and a [link](https://example.com).
| Column A | Column B | Column C |
| --- | --- |
| left | 2 | `x|y` |
```kotlin
fun greet(name: String): String {
// returns a greeting
return "Hello, ${'$'}name!"
}
```
```mermaid
graph TD; A-->B;
```
<div class="html-block">raw html falls back to monospace</div>
---
Done. See CHANGELOG.md.
""".trimIndent()
private val SAMPLE_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()
@Preview(name = "Markdown light", showBackground = true, heightDp = 1400)
@Composable
private fun MarkdownPreviewLight(
@PreviewParameter(MarkdownPreviewParameterProvider::class) text: String,
) {
HapiTheme(darkTheme = false, dynamicColor = false) {
Surface {
Markdown(
text = text,
modifier = Modifier
.verticalScroll(rememberScrollState())
.padding(16.dp),
)
}
}
}
@Preview(name = "Markdown dark", showBackground = true, backgroundColor = 0xFF1C1C1E, heightDp = 1400)
@Composable
private fun MarkdownPreviewDark(
@PreviewParameter(MarkdownPreviewParameterProvider::class) text: String,
) {
HapiTheme(darkTheme = true, dynamicColor = false) {
Surface {
Markdown(
text = text,
modifier = Modifier
.verticalScroll(rememberScrollState())
.padding(16.dp),
)
}
}
}
@Preview(name = "Diff + code OLED", showBackground = true, backgroundColor = 0xFF000000)
@Composable
private fun DiffAndCodeOledPreview() {
HapiTheme(darkTheme = true, dynamicColor = false, oled = true) {
Surface {
val file = remember { UnifiedDiffParser.parse(SAMPLE_DIFF).first() }
Column(
modifier = Modifier.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(12.dp),
) {
DiffView(file = file, compact = true, compactLineLimit = 8)
CodeBlock(
code = "fun sum(a: Int, b: Int) = a + b",
language = "kotlin",
)
}
}
}
}
@@ -0,0 +1,118 @@
package app.hapi.companion.ui.theme
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.graphics.Color
/**
* Semantic color tokens that Material3's scheme does not cover: markdown,
* code, and diff surfaces. Values mirror the web client's CSS custom
* properties (`web/src/index.css`) for its light / dark / OLED themes so both
* clients read the same.
*/
@Immutable
data class HapiExtendedColors(
/** True for the dark and OLED sets -- drives syntax highlight themes. */
val isDark: Boolean,
val codeBackground: Color,
val codeHeaderBackground: Color,
val codeHeaderForeground: Color,
val inlineCodeBackground: Color,
val inlineCodeForeground: Color,
val blockquoteBackground: Color,
val blockquoteBar: Color,
val blockquoteForeground: Color,
val tableBackground: Color,
val tableHeaderBackground: Color,
val divider: Color,
val diffAddBackground: Color,
val diffAddText: Color,
val diffRemoveBackground: Color,
val diffRemoveText: Color,
val diffHunkBackground: Color,
val link: Color,
val hint: Color,
)
/** Mirrors `:root` (light) tokens in web/src/index.css. */
val HapiLightExtendedColors = HapiExtendedColors(
isDark = false,
codeBackground = Color(0xFFF5F6F7),
codeHeaderBackground = Color(0xFFECEFF2),
codeHeaderForeground = Color(0xFF717784),
inlineCodeBackground = Color(0xFFEBECEF),
inlineCodeForeground = Color(0xFF2D333B),
blockquoteBackground = Color(0xFFF1F3F5),
blockquoteBar = Color(0xFFC9D0D8),
blockquoteForeground = Color(0xFF525965),
tableBackground = Color(0xFFF5F6F7),
tableHeaderBackground = Color(0xFFECEFF2),
divider = Color(0xFFE2E5E9),
diffAddBackground = Color(0xFFE6FFED),
diffAddText = Color(0xFF24292E),
diffRemoveBackground = Color(0xFFFFEEF0),
diffRemoveText = Color(0xFF24292E),
diffHunkBackground = Color(0xFFECEFF2),
link = Color(0xFF111827),
hint = Color(0xFF6B7280),
)
/** Mirrors `[data-theme="dark"]` tokens in web/src/index.css. */
val HapiDarkExtendedColors = HapiExtendedColors(
isDark = true,
codeBackground = Color(0xFF2A2F35),
codeHeaderBackground = Color(0xFF353B43),
codeHeaderForeground = Color(0xFFC4CBD6),
inlineCodeBackground = Color(0xFF383E47),
inlineCodeForeground = Color(0xFFF5F7FA),
blockquoteBackground = Color(0xFF31363D),
blockquoteBar = Color(0xFF6B7481),
blockquoteForeground = Color(0xFFD7DDE6),
tableBackground = Color(0xFF2A2F35),
tableHeaderBackground = Color(0xFF353B43),
divider = Color(0xFF3A3F45),
diffAddBackground = Color(0xFF0D2E1F),
diffAddText = Color(0xFFC9D1D9),
diffRemoveBackground = Color(0xFF3F1B23),
diffRemoveText = Color(0xFFC9D1D9),
diffHunkBackground = Color(0xFF353B43),
link = Color(0xFFFFFFFF),
hint = Color(0xFF8E8E93),
)
/**
* Mirrors `[data-theme="oled"]` tokens: pure-black canvas, elevation from
* borders instead of gray fills.
*/
val HapiOledExtendedColors = HapiExtendedColors(
isDark = true,
codeBackground = Color(0xFF0E0E10),
codeHeaderBackground = Color(0xFF161618),
codeHeaderForeground = Color(0xFFC4CBD6),
inlineCodeBackground = Color(0xFF1A1A1D),
inlineCodeForeground = Color(0xFFF5F7FA),
blockquoteBackground = Color(0xFF131316),
blockquoteBar = Color(0xFF3A3A40),
blockquoteForeground = Color(0xFFD7DDE6),
tableBackground = Color(0xFF0E0E10),
tableHeaderBackground = Color(0xFF161618),
divider = Color(0xFF26262A),
diffAddBackground = Color(0xFF07251A),
diffAddText = Color(0xFFC9D1D9),
diffRemoveBackground = Color(0xFF2C1217),
diffRemoveText = Color(0xFFC9D1D9),
diffHunkBackground = Color(0xFF161618),
link = Color(0xFF4EA1FF),
hint = Color(0xFF8E8E93),
)
val LocalHapiExtendedColors = staticCompositionLocalOf { HapiLightExtendedColors }
/** `MaterialTheme.hapi.codeBackground` etc. -- provided by [HapiTheme]. */
val MaterialTheme.hapi: HapiExtendedColors
@Composable
@ReadOnlyComposable
get() = LocalHapiExtendedColors.current
@@ -8,6 +8,7 @@ import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
@@ -23,19 +24,52 @@ private val DarkColorScheme = darkColorScheme(
tertiary = Color(0xFFA0CFD2),
)
/**
* Pure-black scheme for OLED panels (mirrors the web `[data-theme="oled"]`
* token set): the canvas is #000000 and containers stay near-black so
* elevation comes from hairlines, not gray fills. Never combined with
* dynamic color -- Material You surfaces would defeat the point.
*/
private val OledColorScheme = darkColorScheme(
primary = Color(0xFF4EA1FF),
onPrimary = Color(0xFF000000),
secondary = Color(0xFFBCCBB2),
tertiary = Color(0xFFA0CFD2),
background = Color(0xFF000000),
onBackground = Color(0xFFF5F5F7),
surface = Color(0xFF000000),
onSurface = Color(0xFFF5F5F7),
surfaceVariant = Color(0xFF131316),
onSurfaceVariant = Color(0xFFC4CBD6),
surfaceContainerLowest = Color(0xFF000000),
surfaceContainerLow = Color(0xFF0A0A0C),
surfaceContainer = Color(0xFF0E0E10),
surfaceContainerHigh = Color(0xFF131316),
surfaceContainerHighest = Color(0xFF161618),
outline = Color(0xFF3A3A40),
outlineVariant = Color(0xFF26262A),
)
/**
* Material3 theme for the HAPI companion app.
*
* Dynamic color (Material You) on Android 12+, static fallback schemes below.
* M5 revisits this for OLED-black surfaces and full token coverage.
* Also provides [LocalHapiExtendedColors] (markdown/code/diff semantic tokens,
* see [HapiExtendedColors]) mirroring the web light/dark/OLED token sets.
*
* [oled] selects the pure-black variant (implies dark, disables dynamic
* color); the settings screen wires it up later -- callers default to the
* regular schemes.
*/
@Composable
fun HapiTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
dynamicColor: Boolean = true,
oled: Boolean = false,
content: @Composable () -> Unit,
) {
val colorScheme = when {
oled -> OledColorScheme
dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
val context = LocalContext.current
if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
@@ -43,9 +77,16 @@ fun HapiTheme(
darkTheme -> DarkColorScheme
else -> LightColorScheme
}
val extendedColors = when {
oled -> HapiOledExtendedColors
darkTheme -> HapiDarkExtendedColors
else -> HapiLightExtendedColors
}
MaterialTheme(
colorScheme = colorScheme,
content = content,
)
CompositionLocalProvider(LocalHapiExtendedColors provides extendedColors) {
MaterialTheme(
colorScheme = colorScheme,
content = content,
)
}
}
@@ -1,4 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">HAPI</string>
<!-- Markdown / code / diff rendering (B-M2d1) -->
<string name="code_copy">Copy</string>
<string name="code_copied">Copied</string>
<string name="code_plain_fallback">text</string>
<string name="diff_binary_file">Binary file not shown</string>
<string name="diff_show_more">Show %1$d more lines</string>
<string name="diff_collapse">Collapse</string>
</resources>
+9
View File
@@ -22,6 +22,15 @@ kotlin {
dependencies {
api(libs.kotlinx.serialization.json)
// CommonMark parsing lives in this pure-JVM module so parser configuration
// (GFM extensions, disabled indented code blocks) is covered by millisecond
// JVM tests together with the source-level transforms that feed it.
// `api`: :app walks the commonmark AST directly in the Compose renderer.
api(libs.commonmark)
api(libs.commonmark.ext.gfm.tables)
api(libs.commonmark.ext.gfm.strikethrough)
api(libs.commonmark.ext.autolink)
testImplementation(libs.junit)
testImplementation(libs.kotlin.test)
testImplementation(libs.kotlinx.coroutines.test)
@@ -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
}
}
+11
View File
@@ -11,6 +11,10 @@ coreKtx = "1.16.0"
kotlinxSerialization = "1.8.1"
kotlinxCoroutines = "1.10.2"
junit = "4.13.2"
# Markdown / syntax highlighting (B-M2d1). Both verified on Maven Central:
# org.commonmark 0.24.0 (latest, 2024-10) + dev.snipme:highlights 1.0.0 (KMP, jvm variant).
commonmark = "0.24.0"
highlights = "1.0.0"
[libraries]
# AndroidX
@@ -24,6 +28,13 @@ androidx-compose-ui-tooling = { group = "androidx.compose.ui", name = "ui-toolin
androidx-compose-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" }
androidx-compose-material3 = { group = "androidx.compose.material3", name = "material3" }
# Markdown rendering (commonmark-java is pure JVM -- safe for :core:protocol)
commonmark = { group = "org.commonmark", name = "commonmark", version.ref = "commonmark" }
commonmark-ext-gfm-tables = { group = "org.commonmark", name = "commonmark-ext-gfm-tables", version.ref = "commonmark" }
commonmark-ext-gfm-strikethrough = { group = "org.commonmark", name = "commonmark-ext-gfm-strikethrough", version.ref = "commonmark" }
commonmark-ext-autolink = { group = "org.commonmark", name = "commonmark-ext-autolink", version.ref = "commonmark" }
highlights = { group = "dev.snipme", name = "highlights", version.ref = "highlights" }
# KotlinX
kotlinx-serialization-json = { group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-json", version.ref = "kotlinxSerialization" }
kotlinx-coroutines-core = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-core", version.ref = "kotlinxCoroutines" }