Skip to content

Commit ea962d5

Browse files
committed
chore(schema-org): pkg clean up
1 parent 371f021 commit ea962d5

File tree

1 file changed

+21
-35
lines changed

1 file changed

+21
-35
lines changed

packages/schema-org/src/composables/createSchemaOrg.ts

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
1-
import type { App, InjectionKey, Ref } from 'vue-demi'
2-
import { joinURL, withProtocol, withTrailingSlash } from 'ufo'
3-
import { defu } from 'defu'
1+
import type { App, ComputedRef, InjectionKey, Ref } from 'vue-demi'
42
import { computed, ref, unref } from 'vue-demi'
53
import type {
4+
MetaInput,
65
SchemaOrgContext,
76
} from 'schema-org-graph-js'
87
import {
98
buildResolvedGraphCtx,
10-
createSchemaOrgGraph, dedupeAndFlattenNodes, renderNodesToSchemaOrgHtml,
9+
createSchemaOrgGraph, dedupeAndFlattenNodes, renderNodesToSchemaOrgHtml, resolveMeta,
1110
} from 'schema-org-graph-js'
12-
import type {
13-
ConsolaFn,
14-
CreateSchemaOrgInput,
15-
} from '../../types'
11+
12+
export interface CreateSchemaOrgInput {
13+
/**
14+
* The meta data used to render the final schema.org graph.
15+
*/
16+
meta: () => MetaInput
17+
/**
18+
* Client used to write schema to the document.
19+
*/
20+
updateHead: (fn: ComputedRef) => void
21+
/**
22+
* Will enable debug logs to be shown.
23+
*/
24+
debug?: boolean
25+
}
1626

1727
export interface SchemaOrgClient {
1828
install: (app: App) => void
@@ -33,8 +43,6 @@ export interface SchemaOrgClient {
3343
resolveGraph: () => SchemaOrgContext
3444
resolvedSchemaOrg: () => string
3545

36-
debug: ConsolaFn | ((...arg: any) => void)
37-
3846
schemaRef: Ref<string>
3947
ctx: SchemaOrgContext
4048
options: CreateSchemaOrgInput
@@ -49,46 +57,24 @@ const unrefDeep = (n: any) => {
4957
export const PROVIDE_KEY = Symbol('schemaorg') as InjectionKey<SchemaOrgClient>
5058

5159
export const createSchemaOrg = (options: CreateSchemaOrgInput) => {
52-
options = defu(options, {
53-
debug: false,
54-
defaultLanguage: 'en',
55-
})
56-
5760
const schemaRef = ref<string>('')
5861

5962
let ctx = createSchemaOrgGraph()
6063

61-
// eslint-disable-next-line no-console
62-
const debug: ConsolaFn | ((...arg: any) => void) = (...arg: any) => { options.debug && console.debug(...arg) }
63-
const warn: ConsolaFn | ((...arg: any) => void) = (...arg: any) => { console.warn(...arg) }
64-
6564
const client: SchemaOrgClient = {
6665
install(app) {
6766
app.config.globalProperties.$schemaOrg = client
6867
app.provide(PROVIDE_KEY, client)
6968
},
7069

7170
ctx,
72-
debug,
7371
options,
7472
schemaRef,
7573

7674
resolveGraph() {
77-
const meta = unrefDeep(options.meta())
78-
79-
if (meta.host && !meta.canonicalHost)
80-
meta.canonicalHost = meta.host
81-
82-
if (!meta.canonicalHost) {
83-
warn('Missing required `canonicalHost` from `createSchemaOrg`.')
84-
}
85-
else {
86-
// all urls should be fully qualified, such as https://example.com/
87-
meta.canonicalHost = withTrailingSlash(withProtocol(meta.canonicalHost, 'https://'))
88-
}
89-
if (meta.path && !meta.canonicalUrl)
90-
meta.canonicalUrl = joinURL(meta.canonicalHost, meta.path)
91-
75+
const meta = resolveMeta(unrefDeep(options.meta()))
76+
if (!meta.host)
77+
console.warn('[WARN] `@vueuse/schema-org`: Missing required `host` from `createSchemaOrg`.')
9278
return buildResolvedGraphCtx(ctx.nodes.map(unrefDeep), meta)
9379
},
9480

0 commit comments

Comments
 (0)