|
1 | | -import { defineSchemaOrgComponent } from './defineSchemaOrgComponent' |
| 1 | +import { computed, defineComponent, h, ref, unref } from 'vue' |
2 | 2 | // @ts-expect-error untyped |
3 | 3 | import { defineArticle, defineBreadcrumb, defineComment, defineHowTo, defineImage, defineLocalBusiness, defineOrganization, definePerson, defineProduct, defineQuestion, defineRecipe, defineReview, defineVideo, defineWebPage, defineWebSite } from '#vueuse/schema-org/provider' |
| 4 | +// @ts-expect-error untyped |
| 5 | +import { useSchemaOrg } from '#vueuse/schema-org/runtime' |
| 6 | + |
| 7 | +const shallowVNodesToText = (nodes: any) => { |
| 8 | + let text = '' |
| 9 | + for (const node of nodes) { |
| 10 | + if (typeof node.children === 'string') |
| 11 | + text += node.children.trim() |
| 12 | + } |
| 13 | + return text |
| 14 | +} |
| 15 | + |
| 16 | +const fixKey = (s: string) => { |
| 17 | + // kebab case to camel case |
| 18 | + let key = s.replace(/-./g, x => x[1].toUpperCase()) |
| 19 | + // supports @type & @id |
| 20 | + if (key === 'type' || key === 'id') |
| 21 | + key = `@${key}` |
| 22 | + return key |
| 23 | +} |
| 24 | + |
| 25 | +const ignoreKey = (s: string) => { |
| 26 | + // pretty hacky, need to setup all props |
| 27 | + if (s.startsWith('aria-') || s.startsWith('data-')) |
| 28 | + return false |
| 29 | + |
| 30 | + return ['class', 'style'].includes(s) |
| 31 | +} |
| 32 | + |
| 33 | +export const defineSchemaOrgComponent = (name: string, defineFn: (input: any) => any) => { |
| 34 | + return defineComponent({ |
| 35 | + name, |
| 36 | + props: { |
| 37 | + as: String, |
| 38 | + renderScopedSlots: Boolean, |
| 39 | + }, |
| 40 | + setup(props, { slots, attrs }) { |
| 41 | + const node = ref(null) |
| 42 | + |
| 43 | + const nodePartial = computed(() => { |
| 44 | + const val: Record<string, any> = {} |
| 45 | + Object.entries(unref(attrs)).forEach(([key, value]) => { |
| 46 | + if (!ignoreKey(key)) { |
| 47 | + // keys may be passed with kebab case, and they aren't transformed |
| 48 | + val[fixKey(key)] = unref(value) |
| 49 | + } |
| 50 | + }) |
| 51 | + // only render vnodes while we don't have a node |
| 52 | + if (!node.value) { |
| 53 | + // iterate through slots |
| 54 | + for (const [key, slot] of Object.entries(slots)) { |
| 55 | + if (!slot || key === 'default') |
| 56 | + continue |
| 57 | + // allow users to provide data via slots that aren't rendered |
| 58 | + val[fixKey(key)] = shallowVNodesToText(slot(props)) |
| 59 | + } |
| 60 | + } |
| 61 | + return val |
| 62 | + }) |
| 63 | + |
| 64 | + // may not be available |
| 65 | + if (defineFn) { |
| 66 | + // register via main schema composable for route watching |
| 67 | + useSchemaOrg([defineFn(unref(nodePartial))]) |
| 68 | + } |
| 69 | + |
| 70 | + return () => { |
| 71 | + const data = unref(nodePartial) |
| 72 | + // renderless component |
| 73 | + if (!slots.default && !props.renderScopedSlots) |
| 74 | + return null |
| 75 | + const childSlots = [] |
| 76 | + if (slots.default) |
| 77 | + childSlots.push(slots.default(data)) |
| 78 | + if (props.renderScopedSlots) { |
| 79 | + for (const [key, slot] of Object.entries(slots)) { |
| 80 | + if (slot && key !== 'default') |
| 81 | + childSlots.push(slot(data)) |
| 82 | + } |
| 83 | + } |
| 84 | + return h(props.as || 'div', {}, childSlots) |
| 85 | + } |
| 86 | + }, |
| 87 | + }) |
| 88 | +} |
4 | 89 |
|
5 | 90 | export const SchemaOrgArticle = defineSchemaOrgComponent('SchemaOrgArticle', defineArticle) |
6 | 91 | export const SchemaOrgBreadcrumb = defineSchemaOrgComponent('SchemaOrgBreadcrumb', defineBreadcrumb) |
|
0 commit comments