Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change withBase to resolveUrl to resolve trailing slash bug #434

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/schema-org/src/nodes/Article/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe('defineArticle', () => {
expect(client).toMatchInlineSnapshot(`
[
{
"@id": "https://example.com/test/#article",
"@id": "https://example.com/test#article",
"@type": "Article",
"dateModified": "2021-11-10T10:10:10.000Z",
"datePublished": "2021-11-10T10:10:10.000Z",
Expand Down Expand Up @@ -434,7 +434,7 @@ describe('defineArticle', () => {

expect(client[2]).toMatchInlineSnapshot(`
{
"@id": "https://kootingalpecancompany.com/pecan-tree-kootingal/#article",
"@id": "https://kootingalpecancompany.com/pecan-tree-kootingal#article",
"@type": "Article",
"articleSection": [
"Organic pecans, activated pecans, single source, Australian organic pecans",
Expand All @@ -451,7 +451,7 @@ describe('defineArticle', () => {
},
"inLanguage": "en-US",
"isPartOf": {
"@id": "https://kootingalpecancompany.com/pecan-tree-kootingal/#webpage",
"@id": "https://kootingalpecancompany.com/pecan-tree-kootingal#webpage",
},
"keywords": [
"certified organic pecans",
Expand All @@ -461,7 +461,7 @@ describe('defineArticle', () => {
"Pecan tree",
],
"mainEntityOfPage": {
"@id": "https://kootingalpecancompany.com/pecan-tree-kootingal/#webpage",
"@id": "https://kootingalpecancompany.com/pecan-tree-kootingal#webpage",
},
"publisher": {
"@id": "https://kootingalpecancompany.com/#identity",
Expand Down
10 changes: 5 additions & 5 deletions packages/schema-org/src/nodes/Question/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,24 @@ describe('defineQuestion', () => {
expect(graphNodes).toMatchInlineSnapshot(`
[
{
"@id": "https://example.com/frequently-asked-questions/#webpage",
"@id": "https://example.com/frequently-asked-questions#webpage",
"@type": [
"WebPage",
"FAQPage",
],
"mainEntity": [
{
"@id": "https://example.com/frequently-asked-questions/#/schema/question/1dbbae0",
"@id": "https://example.com/frequently-asked-questions#/schema/question/1dbbae0",
},
{
"@id": "https://example.com/frequently-asked-questions/#/schema/question/39eca17",
"@id": "https://example.com/frequently-asked-questions#/schema/question/39eca17",
},
],
"name": "FAQ",
"url": "https://example.com/frequently-asked-questions",
},
{
"@id": "https://example.com/frequently-asked-questions/#/schema/question/1dbbae0",
"@id": "https://example.com/frequently-asked-questions#/schema/question/1dbbae0",
"@type": "Question",
"acceptedAnswer": {
"@type": "Answer",
Expand All @@ -51,7 +51,7 @@ describe('defineQuestion', () => {
"name": "How long is a piece of string?",
},
{
"@id": "https://example.com/frequently-asked-questions/#/schema/question/39eca17",
"@id": "https://example.com/frequently-asked-questions#/schema/question/39eca17",
"@type": "Question",
"acceptedAnswer": {
"@type": "Answer",
Expand Down
4 changes: 2 additions & 2 deletions packages/schema-org/src/nodes/WebPage/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('defineWebPage', () => {

expect(webPage).toMatchInlineSnapshot(`
{
"@id": "https://example.com/test/#webpage",
"@id": "https://example.com/test#webpage",
"@type": "WebPage",
"description": "description",
"name": "headline",
Expand Down Expand Up @@ -156,7 +156,7 @@ describe('defineWebPage', () => {

expect(webpage).toMatchInlineSnapshot(`
{
"@id": "https://example.com/our-pages/about-us/#webpage",
"@id": "https://example.com/our-pages/about-us#webpage",
"@type": [
"WebPage",
"AboutPage",
Expand Down
4 changes: 2 additions & 2 deletions packages/schema-org/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {
Id,
Thing,
} from '../types'
import { hasProtocol, withBase } from 'ufo'
import { cleanDoubleSlashes, hasProtocol, resolveURL, withBase } from 'ufo'

export function idReference<T extends Thing>(node: T | string) {
return {
Expand Down Expand Up @@ -87,7 +87,7 @@ export function prefixId(url: string, id: Id | string) {
return id as Id
if (!id.includes('#'))
id = `#${id}`
return withBase(id, url) as Id
return cleanDoubleSlashes(resolveURL(url, id)) as Id
}

export function trimLength(val: string | undefined, length: number) {
Expand Down
20 changes: 10 additions & 10 deletions test/schema.org/e2e/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('schema.org e2e', () => {
"@context": "https://schema.org",
"@graph": [
{
"@id": "/about/#webpage",
"@id": "/about#webpage",
"name": "About",
"url": "/about",
"@type": [
Expand Down Expand Up @@ -128,7 +128,7 @@ describe('schema.org e2e', () => {
"@context": "https://schema.org",
"@graph": [
{
"@id": "/about/#webpage",
"@id": "/about#webpage",
"name": "About",
"url": "/about",
"@type": [
Expand Down Expand Up @@ -172,18 +172,18 @@ describe('schema.org e2e', () => {
"@context": "https://schema.org",
"@graph": [
{
"@id": "https://example.com/#identity",
"@id": "https://example.com#identity",
"@type": "Organization",
"name": "test",
"url": "https://example.com"
},
{
"@id": "https://example.com/#website",
"@id": "https://example.com#website",
"@type": "WebSite",
"name": "test",
"url": "https://example.com",
"publisher": {
"@id": "https://example.com/#identity"
"@id": "https://example.com#identity"
}
}
]
Expand Down Expand Up @@ -219,23 +219,23 @@ describe('schema.org e2e', () => {
"@context": "https://schema.org",
"@graph": [
{
"@id": "https://example.com/about/#webpage",
"@id": "https://example.com/about#webpage",
"url": "https://example.com/about",
"@type": [
"WebPage",
"FAQPage"
],
"mainEntity": [
{
"@id": "https://example.com/about/#/schema/question/ab1c398"
"@id": "https://example.com/about#/schema/question/ab1c398"
},
{
"@id": "https://example.com/about/#/schema/question/6396da9"
"@id": "https://example.com/about#/schema/question/6396da9"
}
]
},
{
"@id": "https://example.com/about/#/schema/question/ab1c398",
"@id": "https://example.com/about#/schema/question/ab1c398",
"@type": "Question",
"name": "What is your return policy?",
"acceptedAnswer": {
Expand All @@ -244,7 +244,7 @@ describe('schema.org e2e', () => {
}
},
{
"@id": "https://example.com/about/#/schema/question/6396da9",
"@id": "https://example.com/about#/schema/question/6396da9",
"@type": "Question",
"name": "What is something else?",
"acceptedAnswer": {
Expand Down
20 changes: 10 additions & 10 deletions test/schema.org/e2e/no-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('schema.org e2e no plugin', () => {
"@context": "https://schema.org",
"@graph": [
{
"@id": "/about/#webpage",
"@id": "/about#webpage",
"name": "About",
"url": "/about",
"@type": [
Expand Down Expand Up @@ -117,7 +117,7 @@ describe('schema.org e2e no plugin', () => {
"@context": "https://schema.org",
"@graph": [
{
"@id": "/about/#webpage",
"@id": "/about#webpage",
"name": "About",
"url": "/about",
"@type": [
Expand Down Expand Up @@ -164,18 +164,18 @@ describe('schema.org e2e no plugin', () => {
"@context": "https://schema.org",
"@graph": [
{
"@id": "https://example.com/#identity",
"@id": "https://example.com#identity",
"@type": "Organization",
"name": "test",
"url": "https://example.com"
},
{
"@id": "https://example.com/#website",
"@id": "https://example.com#website",
"@type": "WebSite",
"name": "test",
"url": "https://example.com",
"publisher": {
"@id": "https://example.com/#identity"
"@id": "https://example.com#identity"
}
}
]
Expand Down Expand Up @@ -214,23 +214,23 @@ describe('schema.org e2e no plugin', () => {
"@context": "https://schema.org",
"@graph": [
{
"@id": "https://example.com/about/#webpage",
"@id": "https://example.com/about#webpage",
"url": "https://example.com/about",
"@type": [
"WebPage",
"FAQPage"
],
"mainEntity": [
{
"@id": "https://example.com/about/#/schema/question/ab1c398"
"@id": "https://example.com/about#/schema/question/ab1c398"
},
{
"@id": "https://example.com/about/#/schema/question/6396da9"
"@id": "https://example.com/about#/schema/question/6396da9"
}
]
},
{
"@id": "https://example.com/about/#/schema/question/ab1c398",
"@id": "https://example.com/about#/schema/question/ab1c398",
"@type": "Question",
"name": "What is your return policy?",
"acceptedAnswer": {
Expand All @@ -239,7 +239,7 @@ describe('schema.org e2e no plugin', () => {
}
},
{
"@id": "https://example.com/about/#/schema/question/6396da9",
"@id": "https://example.com/about#/schema/question/6396da9",
"@type": "Question",
"name": "What is something else?",
"acceptedAnswer": {
Expand Down
4 changes: 2 additions & 2 deletions test/schema.org/ssr/ids.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('schema.org ssr ids', () => {

const tags = await ssrHead.resolveTags()
const id = JSON.parse(tags[0].innerHTML!)['@graph'][0]['@id']
expect(id).toMatchInlineSnapshot(`"https://example.com/#/schema/web-page/#foo"`)
expect(id).toMatchInlineSnapshot(`"https://example.com#/schema/web-page/#foo"`)
})
it('allows ids with custom domains', async () => {
const ssrHead = createHead()
Expand Down Expand Up @@ -129,7 +129,7 @@ describe('schema.org ssr ids', () => {
],
},
{
"@id": "https://example.com/#webpage",
"@id": "https://example.com#webpage",
"@type": "WebPage",
"isPartOf": {
"@id": "https://example.com/en#website",
Expand Down