feat(settings): convert metadata to text (#2444)
This commit is contained in:
parent
585d8c6f0b
commit
7785f4fe06
|
@ -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 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
|
||||||
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import type { mastodon } from 'masto'
|
import type { mastodon } from 'masto'
|
||||||
import { useForm } from 'slimeform'
|
import { useForm } from 'slimeform'
|
||||||
import { parse } from 'ultrahtml'
|
|
||||||
|
|
||||||
definePageMeta({
|
definePageMeta({
|
||||||
middleware: 'auth',
|
middleware: 'auth',
|
||||||
|
@ -31,9 +30,7 @@ const { form, reset, submitter, isDirty, isError } = useForm({
|
||||||
const fieldsAttributes = Array.from({ length: maxAccountFieldCount.value }, (_, i) => {
|
const fieldsAttributes = Array.from({ length: maxAccountFieldCount.value }, (_, i) => {
|
||||||
const field = { ...account?.fields?.[i] || { name: '', value: '' } }
|
const field = { ...account?.fields?.[i] || { name: '', value: '' } }
|
||||||
|
|
||||||
const linkElement = (parse(field.value)?.children?.[0])
|
field.value = convertMetadata(field.value)
|
||||||
if (linkElement && linkElement?.attributes?.href)
|
|
||||||
field.value = linkElement.attributes.href
|
|
||||||
|
|
||||||
return field
|
return field
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue