From 7785f4fe065a27c90b0077e0f1c53c8f6983895b Mon Sep 17 00:00:00 2001 From: Sma11X <540351143@qq.com> Date: Mon, 27 Nov 2023 20:17:58 +0800 Subject: [PATCH] feat(settings): convert metadata to text (#2444) --- composables/settings/metadata.ts | 27 +++++++++++++++++++++++++++ pages/settings/profile/appearance.vue | 5 +---- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/composables/settings/metadata.ts b/composables/settings/metadata.ts index d0c2357d..703056c2 100644 --- a/composables/settings/metadata.ts +++ b/composables/settings/metadata.ts @@ -1 +1,28 @@ +import type { Node } from 'ultrahtml' +import { decode } from 'tiny-decode' +import { TEXT_NODE, parse } from 'ultrahtml' + export const maxAccountFieldCount = computed(() => isGlitchEdition.value ? 16 : 4) + +export function convertMetadata(metadata: string) { + try { + const tree = parse(metadata) + return (tree.children as Node[]).map(n => convertToText(n)).join('').trim() + } + catch (err) { + console.error(err) + return '' + } +} + +function convertToText(input: Node): string { + let text = '' + + if (input.type === TEXT_NODE) + return decode(input.value) + + if ('children' in input) + text = (input.children as Node[]).map(n => convertToText(n)).join('') + + return text +} diff --git a/pages/settings/profile/appearance.vue b/pages/settings/profile/appearance.vue index aa083200..6538f7e0 100644 --- a/pages/settings/profile/appearance.vue +++ b/pages/settings/profile/appearance.vue @@ -1,7 +1,6 @@