<script setup lang="ts">
import { computed } from 'vue'
import { marked } from 'marked'
import DOMPurify from 'dompurify'

const props = defineProps<{
  content: string
}>()

const html = computed(() => {
  if (!props.content) return ''
  try {
    const raw = marked.parse(props.content, { async: false }) as string
    return DOMPurify.sanitize(raw)
  } catch {
    return props.content
  }
})
</script>

<template>
  <div class="markdown-view" v-html="html" />
</template>

<style lang="scss">
.markdown-view {
  line-height: 1.7;
  word-break: break-word;

  p {
    margin: 0 0 8px;
  }
  h1,
  h2,
  h3,
  h4 {
    margin: 16px 0 8px;
    font-weight: 600;
  }
  ul,
  ol {
    padding-left: 24px;
    margin: 0 0 8px;
  }
  code {
    background: rgba(0, 0, 0, 0.06);
    padding: 2px 6px;
    border-radius: 4px;
    font-family: 'SFMono-Regular', Consolas, monospace;
    font-size: 0.9em;
  }
  pre {
    background: #1e1e1e;
    color: #d4d4d4;
    padding: 12px 16px;
    border-radius: 6px;
    overflow-x: auto;
    margin: 0 0 8px;

    code {
      background: none;
      padding: 0;
      color: inherit;
    }
  }
  table {
    border-collapse: collapse;
    margin: 0 0 8px;

    th,
    td {
      border: 1px solid #dcdfe6;
      padding: 6px 12px;
    }
  }
  blockquote {
    border-left: 4px solid #dcdfe6;
    padding-left: 12px;
    color: #909399;
    margin: 0 0 8px;
  }
  a {
    color: #1890ff;
  }
}
</style>
