From c4e9ce3b14e403518ced74a16b9c7ba0f29c50e8 Mon Sep 17 00:00:00 2001 From: "Fred K. Schott" Date: Wed, 18 Aug 2021 09:05:50 -0700 Subject: [PATCH] fix too strict "relative link" errors in docs website (#1149) --- packages/astro/src/compiler/codegen/index.ts | 8 +++++--- packages/astro/src/compiler/codegen/utils.ts | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/astro/src/compiler/codegen/index.ts b/packages/astro/src/compiler/codegen/index.ts index 96c4f5a74..09bd95b46 100644 --- a/packages/astro/src/compiler/codegen/index.ts +++ b/packages/astro/src/compiler/codegen/index.ts @@ -22,7 +22,7 @@ import { transform } from '../transform/index.js'; import { PRISM_IMPORT } from '../transform/prism.js'; import { nodeBuiltinsSet } from '../../node_builtins.js'; import { readFileSync } from 'fs'; -import { pathToFileURL } from 'url'; +import { fileURLToPath, pathToFileURL } from 'url'; const { parse, FEATURE_CUSTOM_ELEMENT } = astroParser; const traverse: typeof babelTraverse.default = (babelTraverse.default as any).default; @@ -61,6 +61,7 @@ function findHydrationAttributes(attrs: Record): HydrationAttrib /** Retrieve attributes from TemplateNode */ async function getAttributes(nodeName: string, attrs: Attribute[], state: CodegenState, compileOptions: CompileOptions): Promise> { + const isPage = state.filename.startsWith(fileURLToPath(compileOptions.astroConfig.pages)); let result: Record = {}; for (const attr of attrs) { if (attr.type === 'Spread') { @@ -112,8 +113,9 @@ async function getAttributes(nodeName: string, attrs: Attribute[], state: Codege } case 'Text': { let text = getTextFromAttribute(val); - - warnIfRelativeStringLiteral(compileOptions.logging, nodeName, attr, text); + if (!isPage) { + warnIfRelativeStringLiteral(compileOptions.logging, nodeName, attr, text); + } result[attr.name] = JSON.stringify(text); continue; } diff --git a/packages/astro/src/compiler/codegen/utils.ts b/packages/astro/src/compiler/codegen/utils.ts index 9f7c8672e..d32ec9aa2 100644 --- a/packages/astro/src/compiler/codegen/utils.ts +++ b/packages/astro/src/compiler/codegen/utils.ts @@ -24,7 +24,7 @@ export function isImportMetaDeclaration(declaration: VariableDeclarator, metaNam const warnableRelativeValues = new Set(['img+src', 'a+href', 'script+src', 'link+href', 'source+srcset']); -const matchesRelative = /^(?![A-Za-z][+-.0-9A-Za-z]*:|\/)/; +const matchesRelative = /^(?![A-Za-z][+-.0-9A-Za-z]*:|\/|#)/; export function warnIfRelativeStringLiteral(logging: LogOptions, nodeName: string, attr: Attribute, value: string) { let key = nodeName + '+' + attr.name;