refactor(admin-web): move answer markdown normalization to a tested lib
Extract normalizeLooseMarkdownLine from TrackingQuestionDetailView into a shared monitoring-answer-markdown module and harden it (model bullet `**-` and `*` handling). Add a vitest runner and unit tests for the normalization rules. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -17,6 +17,7 @@ import MarkdownPreview from '@/components/MarkdownPreview.vue'
|
||||
import { monitoringApi } from '@/lib/api'
|
||||
import { formatDateTime } from '@/lib/display'
|
||||
import { formatError } from '@/lib/errors'
|
||||
import { normalizeLooseMarkdownLine } from '@/lib/monitoring-answer-markdown'
|
||||
import { isMonitoringPlatformId, normalizeMonitoringPlatformId } from '@/lib/monitoring-platforms'
|
||||
import { useCompanyStore } from '@/stores/company'
|
||||
|
||||
@@ -790,23 +791,6 @@ function sanitizeAnswerBody(answerText: string | null | undefined): string | nul
|
||||
return sanitized || null
|
||||
}
|
||||
|
||||
function normalizeLooseMarkdownLine(line: string): string {
|
||||
const headingNormalizedLine = line
|
||||
.replace(/^[\u200b-\u200f\ufeff]+/, '')
|
||||
.replace(/^([ \t]{0,12})(?:\\#|#|#|#)/i, '$1#')
|
||||
const answerScopedHeading = headingNormalizedLine.replace(
|
||||
/^([ \t]{0,12})#{1,2}(?!#)\s*(?=\S)/,
|
||||
'$1### ',
|
||||
)
|
||||
|
||||
return answerScopedHeading
|
||||
.replace(/^([ \t]{0,12})(#{1,6})(?=\S)/, '$1$2 ')
|
||||
.replace(/^([ \t]{0,12}#{1,6}\s+)#{1,6}\s*/, '$1')
|
||||
.replace(/^([ \t]{0,12}#{1,6}\s+\d+[.)、])(?=\S)/, '$1 ')
|
||||
.replace(/^(\s*)([-*+])(?=\S)/, '$1$2 ')
|
||||
.replace(/^(\s*)(\d{1,3})([.)])(?=\S)/, '$1$2$3 ')
|
||||
}
|
||||
|
||||
function countMarkdownTablePipes(line: string): number {
|
||||
return (line.match(/\|/g) ?? []).length
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user