2023-01-17 18:59:16 +09:00
|
|
|
<script setup lang="ts">
|
|
|
|
import type { mastodon } from 'masto'
|
|
|
|
|
|
|
|
const props = defineProps<{
|
|
|
|
card: mastodon.v1.PreviewCard
|
|
|
|
/** For the preview image, only the small image mode is displayed */
|
|
|
|
smallPictureOnly?: boolean
|
|
|
|
/** When it is root card in the list, not appear as a child card */
|
|
|
|
root?: boolean
|
|
|
|
}>()
|
|
|
|
|
|
|
|
interface Meta {
|
|
|
|
code?: string
|
|
|
|
file?: string
|
|
|
|
lines?: string
|
|
|
|
project?: string
|
|
|
|
}
|
|
|
|
|
2023-01-17 20:47:52 +09:00
|
|
|
// Protect against long code snippets
|
|
|
|
const maxLines = 20
|
|
|
|
|
2024-02-22 00:20:08 +09:00
|
|
|
const meta = computed(() => {
|
2023-01-17 18:59:16 +09:00
|
|
|
const { description } = props.card
|
2023-02-02 03:01:38 +09:00
|
|
|
const meta = description.match(/.*Code Snippet from (.+), lines (\S+)\n\n(.+)/s)
|
2023-01-17 18:59:16 +09:00
|
|
|
const file = meta?.[1]
|
2023-02-02 03:01:38 +09:00
|
|
|
const lines = meta?.[2]
|
2023-01-17 20:47:52 +09:00
|
|
|
const code = meta?.[3].split('\n').slice(0, maxLines).join('\n')
|
2023-01-17 18:59:16 +09:00
|
|
|
const project = props.card.title?.replace(' - StackBlitz', '')
|
2024-02-25 01:46:14 +09:00
|
|
|
return {
|
2023-01-17 18:59:16 +09:00
|
|
|
file,
|
|
|
|
lines,
|
|
|
|
code,
|
|
|
|
project,
|
2024-02-25 01:46:14 +09:00
|
|
|
} satisfies Meta
|
2023-01-17 18:59:16 +09:00
|
|
|
})
|
|
|
|
|
2024-02-22 00:20:08 +09:00
|
|
|
const vnodeCode = computed(() => {
|
|
|
|
if (!meta.value.code)
|
2023-01-17 18:59:16 +09:00
|
|
|
return null
|
2024-02-22 00:20:08 +09:00
|
|
|
const code = meta.value.code
|
2023-02-02 03:01:38 +09:00
|
|
|
.replace(/</g, '<')
|
|
|
|
.replace(/>/g, '>')
|
|
|
|
.replace(/`/g, '`')
|
|
|
|
|
2024-02-22 00:20:08 +09:00
|
|
|
const vnode = contentToVNode(`<p>\`\`\`${meta.value.file?.split('.')?.[1] ?? ''}\n${code}\n\`\`\`\</p>`, {
|
2023-01-17 18:59:16 +09:00
|
|
|
markdown: true,
|
|
|
|
})
|
|
|
|
return vnode
|
|
|
|
})
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
|
|
|
<div
|
|
|
|
v-if="meta.code"
|
|
|
|
flex flex-col gap-1
|
|
|
|
display-block of-hidden
|
|
|
|
w-full
|
|
|
|
rounded-lg
|
|
|
|
overflow-hidden
|
|
|
|
pb-2
|
|
|
|
>
|
|
|
|
<div whitespace-pre-wrap break-words>
|
|
|
|
<span v-if="vnodeCode" class="content-rich line-compact" dir="auto">
|
|
|
|
<component :is="vnodeCode" />
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
<div
|
2023-01-17 19:54:25 +09:00
|
|
|
flex
|
|
|
|
justify-between
|
2023-01-17 18:59:16 +09:00
|
|
|
display-block of-hidden
|
|
|
|
bg-card
|
|
|
|
w-full
|
|
|
|
p-3
|
|
|
|
pb-4
|
|
|
|
>
|
2023-01-17 19:54:25 +09:00
|
|
|
<div flex flex-col>
|
2023-01-17 18:59:16 +09:00
|
|
|
<p flex gap-1>
|
2023-01-17 19:54:25 +09:00
|
|
|
<span>{{ $t('custom_cards.stackblitz.snippet_from', [meta.file]) }}</span><span text-secondary>{{ `- ${$t('custom_cards.stackblitz.lines', [meta.lines])}` }}</span>
|
2023-01-17 18:59:16 +09:00
|
|
|
</p>
|
2023-01-17 19:54:25 +09:00
|
|
|
<div flex font-bold gap-2>
|
|
|
|
<span text-primary>{{ meta.project }}</span><span flex text-secondary><span flex items-center><svg h-5 width="22.27" height="32" viewBox="0 0 256 368"><path fill="currentColor" d="M109.586 217.013H0L200.34 0l-53.926 150.233H256L55.645 367.246l53.927-150.233z" /></svg></span><span>StackBlitz</span></span>
|
|
|
|
</div>
|
2023-01-17 18:59:16 +09:00
|
|
|
</div>
|
2023-01-17 19:54:25 +09:00
|
|
|
<NuxtLink external target="_blank" btn-solid pt-0 pb-1 px-2 h-fit :to="card.url">
|
|
|
|
{{ $t('custom_cards.stackblitz.open') }}
|
|
|
|
</NuxtLink>
|
2023-01-17 18:59:16 +09:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<StatusPreviewCardNormal v-else :card="card" :small-picture-only="smallPictureOnly" :root="root" />
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
.content-rich p {
|
|
|
|
margin-top: 0;
|
|
|
|
}
|
|
|
|
.code-block {
|
|
|
|
margin-top: 0;
|
|
|
|
border-radius: 0;
|
|
|
|
}
|
|
|
|
</style>
|