notes: feat: markdown formatting in edit & view modes
- Commit
8f5fa675d584b8a43e805bf304e1e255c62d085e- Author
- Vayun Mathur <[email protected]>
- When
- 2026-02-16 21:16 UTC (9 months ago)
- Parents
69e70d9
Diff
notes/src/main/java/com/vayunmathur/notes/MarkdownAnnotatedString.kt
diff --git a/notes/src/main/java/com/vayunmathur/notes/MarkdownAnnotatedString.kt b/notes/src/main/java/com/vayunmathur/notes/MarkdownAnnotatedString.ktnew file mode 100644index 0000000000..1e1851c771--- /dev/null+++ b/notes/src/main/java/com/vayunmathur/notes/MarkdownAnnotatedString.kt@@ -0,0 +1,166 @@+package com.vayunmathur.notes++import androidx.compose.ui.graphics.Color+import androidx.compose.ui.text.AnnotatedString+import androidx.compose.ui.text.ParagraphStyle+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.FontStyle+import androidx.compose.ui.text.font.FontWeight+import androidx.compose.ui.text.style.LineHeightStyle+import androidx.compose.ui.text.style.TextDecoration+import androidx.compose.ui.text.style.TextGeometricTransform+import androidx.compose.ui.unit.sp++/**+ * Converts a Markdown string into an AnnotatedString for Jetpack Compose.+ * @param mdtext The raw markdown text.+ * @param showMarkers If false, the formatting symbols (#, *, etc.) are hidden and occupy no space.+ */+fun MarkdownAnnotatedString(mdtext: String, showMarkers: Boolean = true): AnnotatedString {+ return buildAnnotatedString {+ append(mdtext)++ // Helper to hide formatting markers if showMarkers is false.+ // We use TextGeometricTransform with scaleX = 0f to collapse the horizontal width.+ fun hideRange(start: Int, end: Int) {+ if (!showMarkers && start < end) {+ addStyle(+ style = SpanStyle(+ color = Color.Transparent,+ fontSize = 0.sp,+ textGeometricTransform = TextGeometricTransform(scaleX = 0f)+ ),+ start = start,+ end = end+ )+ }+ }++ // 1. Headers (e.g., # Heading)+ val headerRegex = Regex("(?m)^(#{1,6} )(.*(?:\\R|$))")+ headerRegex.findAll(mdtext).forEach { match ->+ val start = match.range.first+ val end = match.range.last + 1++ val markers = match.groups[1]!! // The "### " part+ val content = match.groups[2]!! // The text + newline++ val level = markers.value.trim().length+ val fontSize = (32 - (level * 2)).sp++ // Hide the hashes and the space if requested+ hideRange(markers.range.first, markers.range.last + 1)++ // SpanStyle for the text content+ addStyle(+ style = SpanStyle(+ fontWeight = FontWeight.Bold,+ fontSize = fontSize,+ ),+ start = start,+ end = end+ )++ // ParagraphStyle for spacing (using 0.6f multiplier as requested)+ addStyle(+ style = ParagraphStyle(+ lineHeight = fontSize * 0.6f,+ lineHeightStyle = LineHeightStyle(+ alignment = LineHeightStyle.Alignment.Center,+ trim = LineHeightStyle.Trim.None+ )+ ),+ start = start,+ end = end+ )+ }++ // 2. Bold (**text** or __text__)+ val boldRegex = Regex("(\\*\\*|__)(.*?)\\1")+ boldRegex.findAll(mdtext).forEach { match ->+ val markerType = match.groups[1]!!.value+ val start = match.range.first+ val end = match.range.last + 1++ hideRange(start, start + markerType.length)+ hideRange(end - markerType.length, end)++ addStyle(+ style = SpanStyle(fontWeight = FontWeight.Bold),+ start = start,+ end = end+ )+ }++ // 3. Italic (*text* or _text_)+ val italicRegex = Regex("(?<!\\*)\\*(?!\\*)(.*?)(?<!\\*)\\*(?!\\*)|(?<!_)_(?!_)(.*?)(?<!_)_(?!_)")+ italicRegex.findAll(mdtext).forEach { match ->+ val start = match.range.first+ val end = match.range.last + 1++ hideRange(start, start + 1)+ hideRange(end - 1, end)++ addStyle(+ style = SpanStyle(fontStyle = FontStyle.Italic),+ start = start,+ end = end+ )+ }++ // 4. Inline Code (`code`)+ val codeRegex = Regex("`(.+?)`")+ codeRegex.findAll(mdtext).forEach { match ->+ val start = match.range.first+ val end = match.range.last + 1++ hideRange(start, start + 1)+ hideRange(end - 1, end)++ addStyle(+ style = SpanStyle(+ fontFamily = FontFamily.Monospace,+ background = Color.LightGray.copy(alpha = 0.2f),+ color = Color(0xFFD32F2F)+ ),+ start = start,+ end = end+ )+ }++ // 5. Strikethrough (~~text~~)+ val strikeRegex = Regex("~~(.+?)~~")+ strikeRegex.findAll(mdtext).forEach { match ->+ val start = match.range.first+ val end = match.range.last + 1++ hideRange(start, start + 2)+ hideRange(end - 2, end)++ addStyle(+ style = SpanStyle(textDecoration = TextDecoration.LineThrough),+ start = start,+ end = end+ )+ }++ // 6. Blockquotes+ val quoteRegex = Regex("(?m)^>\\s")+ quoteRegex.findAll(mdtext).forEach { match ->+ hideRange(match.range.first, match.range.last + 1)++ val lineEnd = mdtext.indexOf('\n', match.range.first).let { if (it == -1) mdtext.length else it }++ addStyle(+ style = SpanStyle(+ color = Color.Gray,+ fontStyle = FontStyle.Italic+ ),+ start = match.range.first,+ end = lineEnd+ )+ }+ }+}\ No newline at end of file
notes/src/main/java/com/vayunmathur/notes/ui/NotePage.kt
diff --git a/notes/src/main/java/com/vayunmathur/notes/ui/NotePage.kt b/notes/src/main/java/com/vayunmathur/notes/ui/NotePage.ktindex 164d065afc..08cf60a79b 100644--- a/notes/src/main/java/com/vayunmathur/notes/ui/NotePage.kt+++ b/notes/src/main/java/com/vayunmathur/notes/ui/NotePage.kt@@ -8,6 +8,7 @@ import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.text.BasicTextField+import androidx.compose.foundation.text.input.rememberTextFieldState import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.IconButton import androidx.compose.material3.LocalContentColor@@ -16,20 +17,25 @@ import androidx.compose.material3.Scaffold import androidx.compose.material3.Text import androidx.compose.material3.TopAppBar import androidx.compose.runtime.Composable+import androidx.compose.runtime.derivedStateOf import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.SolidColor+import androidx.compose.ui.text.AnnotatedString+import androidx.compose.ui.text.input.TextFieldValue import androidx.compose.ui.unit.dp import androidx.navigation3.runtime.NavBackStack+import androidx.room.util.TableInfo import com.vayunmathur.library.ui.IconDelete import com.vayunmathur.library.ui.IconEdit import com.vayunmathur.library.ui.IconNavigation import com.vayunmathur.library.ui.IconVisible import com.vayunmathur.library.util.DatabaseViewModel import com.vayunmathur.library.util.pop+import com.vayunmathur.notes.MarkdownAnnotatedString import com.vayunmathur.notes.Route import com.vayunmathur.notes.data.Note @@ -66,17 +72,25 @@ fun NotePage(backStack: NavBackStack<Route>, viewModel: DatabaseViewModel, noteI readOnly = !isEditing, textStyle = MaterialTheme.typography.headlineMedium.copy(color = LocalContentColor.current), cursorBrush = SolidColor(LocalContentColor.current),- decorationBox = {innerTextField ->+ decorationBox = { innerTextField -> Box {- if(note.title.isEmpty()) Text(text = "Title", style = MaterialTheme.typography.headlineMedium.copy(color = MaterialTheme.colorScheme.onSurfaceVariant))+ if (note.title.isEmpty()) Text(+ text = "Title",+ style = MaterialTheme.typography.headlineMedium.copy(color = MaterialTheme.colorScheme.onSurfaceVariant)+ ) innerTextField() } } ) Spacer(Modifier.height(8.dp))+ var value by remember(note.id) { mutableStateOf(TextFieldValue(MarkdownAnnotatedString(note.content))) }+ val noMarkers by remember { derivedStateOf { TextFieldValue(MarkdownAnnotatedString(note.content, false)) } } BasicTextField(- note.content,- { note = note.copy(content = it) },+ if(isEditing) value else noMarkers,+ {+ note = note.copy(content = it.text)+ value = it.copy(annotatedString = MarkdownAnnotatedString(it.text))+ }, Modifier.fillMaxSize(), readOnly = !isEditing, textStyle = MaterialTheme.typography.bodyMedium.copy(color = LocalContentColor.current),
youpipe/schemas/com.vayunmathur.youpipe.data.SubscriptionDatabase/1.json
diff --git a/youpipe/schemas/com.vayunmathur.youpipe.data.SubscriptionDatabase/1.json b/youpipe/schemas/com.vayunmathur.youpipe.data.SubscriptionDatabase/1.jsonnew file mode 100644index 0000000000..4a3b647a15--- /dev/null+++ b/youpipe/schemas/com.vayunmathur.youpipe.data.SubscriptionDatabase/1.json@@ -0,0 +1,289 @@+{+ "formatVersion": 1,+ "database": {+ "version": 1,+ "identityHash": "3df2a06f39a597270fb8c2f9e01fe93f",+ "entities": [+ {+ "tableName": "Subscription",+ "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `name` TEXT NOT NULL, `channelID` TEXT NOT NULL, `avatarURL` TEXT NOT NULL, `uploadsPlaylistID` TEXT NOT NULL, `position` REAL NOT NULL)",+ "fields": [+ {+ "fieldPath": "id",+ "columnName": "id",+ "affinity": "INTEGER",+ "notNull": true+ },+ {+ "fieldPath": "name",+ "columnName": "name",+ "affinity": "TEXT",+ "notNull": true+ },+ {+ "fieldPath": "channelID",+ "columnName": "channelID",+ "affinity": "TEXT",+ "notNull": true+ },+ {+ "fieldPath": "avatarURL",+ "columnName": "avatarURL",+ "affinity": "TEXT",+ "notNull": true+ },+ {+ "fieldPath": "uploadsPlaylistID",+ "columnName": "uploadsPlaylistID",+ "affinity": "TEXT",+ "notNull": true+ },+ {+ "fieldPath": "position",+ "columnName": "position",+ "affinity": "REAL",+ "notNull": true+ }+ ],+ "primaryKey": {+ "autoGenerate": true,+ "columnNames": [+ "id"+ ]+ }+ },+ {+ "tableName": "SubscriptionVideo",+ "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `name` TEXT NOT NULL, `duration` INTEGER NOT NULL, `views` INTEGER NOT NULL, `uploadDate` INTEGER NOT NULL, `thumbnailURL` TEXT NOT NULL, `author` TEXT NOT NULL, `channelID` INTEGER NOT NULL, `position` REAL NOT NULL, FOREIGN KEY(`channelID`) REFERENCES `Subscription`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )",+ "fields": [+ {+ "fieldPath": "id",+ "columnName": "id",+ "affinity": "INTEGER",+ "notNull": true+ },+ {+ "fieldPath": "name",+ "columnName": "name",+ "affinity": "TEXT",+ "notNull": true+ },+ {+ "fieldPath": "duration",+ "columnName": "duration",+ "affinity": "INTEGER",+ "notNull": true+ },+ {+ "fieldPath": "views",+ "columnName": "views",+ "affinity": "INTEGER",+ "notNull": true+ },+ {+ "fieldPath": "uploadDate",+ "columnName": "uploadDate",+ "affinity": "INTEGER",+ "notNull": true+ },+ {+ "fieldPath": "thumbnailURL",+ "columnName": "thumbnailURL",+ "affinity": "TEXT",+ "notNull": true+ },+ {+ "fieldPath": "author",+ "columnName": "author",+ "affinity": "TEXT",+ "notNull": true+ },+ {+ "fieldPath": "channelID",+ "columnName": "channelID",+ "affinity": "INTEGER",+ "notNull": true+ },+ {+ "fieldPath": "position",+ "columnName": "position",+ "affinity": "REAL",+ "notNull": true+ }+ ],+ "primaryKey": {+ "autoGenerate": true,+ "columnNames": [+ "id"+ ]+ },+ "indices": [+ {+ "name": "index_SubscriptionVideo_channelID",+ "unique": false,+ "columnNames": [+ "channelID"+ ],+ "orders": [],+ "createSql": "CREATE INDEX IF NOT EXISTS `index_SubscriptionVideo_channelID` ON `${TABLE_NAME}` (`channelID`)"+ }+ ],+ "foreignKeys": [+ {+ "table": "Subscription",+ "onDelete": "CASCADE",+ "onUpdate": "NO ACTION",+ "columns": [+ "channelID"+ ],+ "referencedColumns": [+ "id"+ ]+ }+ ]+ },+ {+ "tableName": "HistoryVideo",+ "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `progress` INTEGER NOT NULL, `timestamp` INTEGER NOT NULL, `position` REAL NOT NULL, `name` TEXT NOT NULL, `videoID` INTEGER NOT NULL, `duration` INTEGER NOT NULL, `views` INTEGER NOT NULL, `uploadDate` INTEGER NOT NULL, `thumbnailURL` TEXT NOT NULL, `author` TEXT NOT NULL)",+ "fields": [+ {+ "fieldPath": "id",+ "columnName": "id",+ "affinity": "INTEGER",+ "notNull": true+ },+ {+ "fieldPath": "progress",+ "columnName": "progress",+ "affinity": "INTEGER",+ "notNull": true+ },+ {+ "fieldPath": "timestamp",+ "columnName": "timestamp",+ "affinity": "INTEGER",+ "notNull": true+ },+ {+ "fieldPath": "position",+ "columnName": "position",+ "affinity": "REAL",+ "notNull": true+ },+ {+ "fieldPath": "videoItem.name",+ "columnName": "name",+ "affinity": "TEXT",+ "notNull": true+ },+ {+ "fieldPath": "videoItem.videoID",+ "columnName": "videoID",+ "affinity": "INTEGER",+ "notNull": true+ },+ {+ "fieldPath": "videoItem.duration",+ "columnName": "duration",+ "affinity": "INTEGER",+ "notNull": true+ },+ {+ "fieldPath": "videoItem.views",+ "columnName": "views",+ "affinity": "INTEGER",+ "notNull": true+ },+ {+ "fieldPath": "videoItem.uploadDate",+ "columnName": "uploadDate",+ "affinity": "INTEGER",+ "notNull": true+ },+ {+ "fieldPath": "videoItem.thumbnailURL",+ "columnName": "thumbnailURL",+ "affinity": "TEXT",+ "notNull": true+ },+ {+ "fieldPath": "videoItem.author",+ "columnName": "author",+ "affinity": "TEXT",+ "notNull": true+ }+ ],+ "primaryKey": {+ "autoGenerate": true,+ "columnNames": [+ "id"+ ]+ }+ },+ {+ "tableName": "SubscriptionCategory",+ "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`subscriptionID` INTEGER NOT NULL, `category` TEXT NOT NULL, `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `position` REAL NOT NULL, FOREIGN KEY(`subscriptionID`) REFERENCES `Subscription`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )",+ "fields": [+ {+ "fieldPath": "subscriptionID",+ "columnName": "subscriptionID",+ "affinity": "INTEGER",+ "notNull": true+ },+ {+ "fieldPath": "category",+ "columnName": "category",+ "affinity": "TEXT",+ "notNull": true+ },+ {+ "fieldPath": "id",+ "columnName": "id",+ "affinity": "INTEGER",+ "notNull": true+ },+ {+ "fieldPath": "position",+ "columnName": "position",+ "affinity": "REAL",+ "notNull": true+ }+ ],+ "primaryKey": {+ "autoGenerate": true,+ "columnNames": [+ "id"+ ]+ },+ "indices": [+ {+ "name": "index_SubscriptionCategory_subscriptionID",+ "unique": false,+ "columnNames": [+ "subscriptionID"+ ],+ "orders": [],+ "createSql": "CREATE INDEX IF NOT EXISTS `index_SubscriptionCategory_subscriptionID` ON `${TABLE_NAME}` (`subscriptionID`)"+ }+ ],+ "foreignKeys": [+ {+ "table": "Subscription",+ "onDelete": "CASCADE",+ "onUpdate": "NO ACTION",+ "columns": [+ "subscriptionID"+ ],+ "referencedColumns": [+ "id"+ ]+ }+ ]+ }+ ],+ "setupQueries": [+ "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",+ "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '3df2a06f39a597270fb8c2f9e01fe93f')"+ ]+ }+}\ No newline at end of file