Skip to content

Commit

Permalink
feat(desc-text): add linkify option
Browse files Browse the repository at this point in the history
  • Loading branch information
brc-dd committed Sep 25, 2024
1 parent 89a9e8d commit 5ba10ba
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
20 changes: 16 additions & 4 deletions lib/components/SDescText.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<script setup lang="ts">
import MarkdownIt from 'markdown-it'

Check failure on line 2 in lib/components/SDescText.vue

View workflow job for this annotation

GitHub Actions / test

'MarkdownIt' is defined but never used

Check failure on line 2 in lib/components/SDescText.vue

View workflow job for this annotation

GitHub Actions / test

'MarkdownIt' is defined but never used
import { useLinkifyIt } from 'sefirot/composables/Markdown'
import { computed } from 'vue'
import { useHasSlotContent } from '../composables/Utils'
import SDescEmpty from './SDescEmpty.vue'
Expand All @@ -7,6 +9,7 @@ const props = defineProps<{
value?: string | null
lineClamp?: string | number
preWrap?: boolean
linkify?: boolean
}>()
const classes = computed(() => [
Expand All @@ -17,14 +20,23 @@ const classes = computed(() => [
const hasSlot = useHasSlotContent()
const lineClamp = computed(() => props.lineClamp ?? 'none')
const linkifyIt = useLinkifyIt()
const _value = computed(() => {
if (props.linkify) {
return linkifyIt(props.value ?? '')
}
return props.value
})
</script>

<template>
<div v-if="hasSlot || value" class="SDescText" :class="classes">
<div class="value">
<slot v-if="hasSlot" />
<template v-else>{{ value }}</template>
<div v-if="hasSlot || _value" class="SDescText" :class="classes">
<div class="value" v-if="hasSlot">
<slot />
</div>
<div class="value" v-else v-html="_value" />
</div>
<SDescEmpty v-else />
</template>
Expand Down
9 changes: 9 additions & 0 deletions lib/composables/Markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ export function useMarkdown(options: UseMarkdownOptions = {}): UseMarkdown {
}
}

export function useLinkifyIt() {
const md = new MarkdownIt('zero', { linkify: true })
md.enable('linkify')

return (source: string) => {
return md.renderInline(source)
}
}

export interface UseLink {
addListeners(): void
removeListeners(): void
Expand Down

0 comments on commit 5ba10ba

Please sign in to comment.