Merge pull request #6762 from ouchihao/codex/issue-2045-payment-help-markdown

feat(payment): render help text as sanitized Markdown
This commit is contained in:
Wesley Liddick authored and GitHub committed 2026-09-08 20:11:56 +08:00
commit fa556700c2
2 files changed
+71 -1

No files matched your search

+8 -1
View File
@@ -222,7 +222,7 @@
<img v-if="checkout.help_image_url" :src="checkout.help_image_url" alt=""
class="h-40 max-w-full cursor-pointer rounded-lg object-contain transition-opacity hover:opacity-80"
@click="previewImage = checkout.help_image_url" />
<p v-if="checkout.help_text" class="text-center text-sm text-gray-500 dark:text-gray-400">{{ checkout.help_text }}</p>
<div v-if="checkout.help_text" class="markdown-body w-full overflow-x-auto break-words" v-html="renderedHelpText"></div>
</div>
</div>
</template>
@@ -258,6 +258,9 @@
<script setup lang="ts">
import { ref, computed, onMounted, watch } from 'vue'
import { useI18n } from 'vue-i18n'
import { marked } from 'marked'
import DOMPurify from 'dompurify'
import '@/styles/announcement-markdown.css'
import { useRoute, useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { usePaymentStore } from '@/stores/payment'
@@ -505,6 +508,10 @@ const checkout = ref<CheckoutInfoResponse>({
plans: [], balance_disabled: false, balance_recharge_multiplier: 1, subscription_usd_to_cny_rate: 0, recharge_fee_rate: 0, help_text: '', help_image_url: '', stripe_publishable_key: '',
})
const renderedHelpText = computed(() => DOMPurify.sanitize(
marked.parse(checkout.value.help_text || '', { async: false, gfm: true, breaks: false }),
))
const tabs = computed(() => {
const result: { key: 'recharge' | 'subscription'; label: string }[] = []
if (!checkout.value.balance_disabled) result.push({ key: 'recharge', label: t('payment.tabTopUp') })
@@ -281,6 +281,69 @@ async function mountSubscriptionPlanList(planCount: number) {
return wrapper
}
describe('PaymentView help text', () => {
beforeEach(() => {
vi.useRealTimers()
routeState.path = '/purchase'
routeState.query = {}
createOrder.mockReset()
window.localStorage.clear()
})
async function mountHelp(help_text: string, help_image_url = '') {
getCheckoutInfo.mockReset().mockResolvedValue(checkoutInfoFixture({ help_text, help_image_url }))
const wrapper = shallowMount(PaymentView, {
global: {
stubs: {
AppLayout: { template: '<div><slot /></div>' },
Teleport: true,
Transition: false,
},
},
})
await flushPromises()
return wrapper
}
it('renders headings, emphasis, links, and lists in payment help without starting checkout', async () => {
const wrapper = await mountHelp('## Recharge help\n\n**Read first**\n\n- [Contact support](https://example.com/help)')
const help = wrapper.get('.markdown-body')
expect(help.get('h2').text()).toBe('Recharge help')
expect(help.get('strong').text()).toBe('Read first')
expect(help.get('li a').attributes('href')).toBe('https://example.com/help')
expect(createOrder).not.toHaveBeenCalled()
})
it('removes scripts, event handlers, and unsafe URLs from rendered help', async () => {
const wrapper = await mountHelp([
'<script>alert(1)</script>',
'<img src="https://example.com/help.png" onerror="alert(1)">',
'[Unsafe](javascript:alert%281%29)',
'[Support](https://example.com/help)',
].join('\n\n'))
const help = wrapper.get('.markdown-body')
expect(help.find('script').exists()).toBe(false)
expect(help.get('img').attributes('onerror')).toBeUndefined()
expect(help.findAll('a').map(link => link.attributes('href'))).toEqual([undefined, 'https://example.com/help'])
})
it('keeps plain-text soft line breaks and the separate help image preview', async () => {
const wrapper = await mountHelp('First line\nSecond line', 'https://example.com/help.png')
const help = wrapper.get('.markdown-body')
expect(help.get('p').text()).toBe('First line\nSecond line')
expect(help.find('br').exists()).toBe(false)
await wrapper.get('img').trigger('click')
expect(wrapper.findAll('img')).toHaveLength(2)
expect(wrapper.findAll('img')[1].attributes('src')).toBe('https://example.com/help.png')
})
it('keeps image-only help without an empty Markdown container', async () => {
const wrapper = await mountHelp('', 'https://example.com/help.png')
expect(wrapper.find('.markdown-body').exists()).toBe(false)
expect(wrapper.get('img').attributes('src')).toBe('https://example.com/help.png')
})
})
describe('PaymentView subscription plan grid', () => {
it.each([3, 4, 6])('keeps %i plans on the existing mobile/tablet/desktop grid', async (planCount) => {
const wrapper = await mountSubscriptionPlanList(planCount)