diff --git a/.changeset/plenty-singers-divide.md b/.changeset/plenty-singers-divide.md new file mode 100644 index 000000000000..0188051c4134 --- /dev/null +++ b/.changeset/plenty-singers-divide.md @@ -0,0 +1,9 @@ +--- +'astro': patch +'@astrojs/markdown-remark': patch +'@astrojs/mdx': patch +'@astrojs/markdoc': patch +'@astrojs/internal-helpers': minor +--- + +Adds YAML parsing with timestamp and merge key support, and formats YAML errors consistently across Astro, Markdown, MDX, and Markdoc. diff --git a/packages/astro/package.json b/packages/astro/package.json index 60f9cc70fea0..3d885e1f62a8 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -150,7 +150,6 @@ "github-slugger": "^2.0.0", "html-escaper": "3.0.3", "http-cache-semantics": "^4.2.0", - "js-yaml": "^4.3.2", "jsonc-parser": "^3.3.1", "magic-string": "^1.0.0", "magicast": "^0.5.2", @@ -197,7 +196,6 @@ "@types/aria-query": "^5.0.4", "@types/html-escaper": "3.0.4", "@types/http-cache-semantics": "^4.2.0", - "@types/js-yaml": "^4.0.9", "@types/parse-srcset": "^1.0.0", "@types/picomatch": "^4.0.2", "@types/yargs-parser": "^21.0.3", diff --git a/packages/astro/src/content/loaders/file.ts b/packages/astro/src/content/loaders/file.ts index dc9e8fd1b359..f42bc103317a 100644 --- a/packages/astro/src/content/loaders/file.ts +++ b/packages/astro/src/content/loaders/file.ts @@ -1,6 +1,6 @@ import { existsSync, promises as fs } from 'node:fs'; import { fileURLToPath } from 'node:url'; -import * as yaml from 'js-yaml'; +import { parseYaml } from '@astrojs/internal-helpers/yaml'; import * as toml from 'smol-toml'; import { DuplicateContentEntrySlugError, @@ -37,10 +37,7 @@ export function file(fileName: string, options?: FileOptions): Loader { if (ext === 'json') { parse = JSON.parse; } else if (ext === 'yml' || ext === 'yaml') { - parse = (text) => - yaml.load(text, { - filename: fileName, - }); + parse = parseYaml; } else if (ext === 'toml') { parse = toml.parse; } diff --git a/packages/astro/src/content/utils.ts b/packages/astro/src/content/utils.ts index 46c9c8890dc8..3889614ae1da 100644 --- a/packages/astro/src/content/utils.ts +++ b/packages/astro/src/content/utils.ts @@ -8,7 +8,7 @@ import type { RunnableDevEnvironment, Rolldown } from 'vite'; import xxhash from 'xxhash-wasm'; import * as z from 'zod/v4'; import { AstroError, AstroErrorData, errorMap, MarkdownError } from '../core/errors/index.js'; -import { isYAMLException } from '../core/errors/utils.js'; +import { isYAMLParseError } from '../core/errors/utils.js'; import type { AstroLogger } from '../core/logger/core.js'; import { appendForwardSlash } from '../core/path.js'; import { normalizePath } from '../core/viteUtils.js'; @@ -461,14 +461,17 @@ export function safeParseFrontmatter(source: string, id?: string) { : undefined, }); - if (isYAMLException(err)) { - markdownError.setLocation({ - file: id, - line: err.mark.line, - column: err.mark.column, - }); + if (isYAMLParseError(err)) { + const position = err.linePos?.[0]; + if (position) { + markdownError.setLocation({ + file: id, + line: position.line - 1, + column: position.col - 1, + }); + } - markdownError.setMessage(err.reason); + markdownError.setMessage(err.message); } throw markdownError; diff --git a/packages/astro/src/core/config/settings.ts b/packages/astro/src/core/config/settings.ts index 773c9d01c336..0e92f3d3cc18 100644 --- a/packages/astro/src/core/config/settings.ts +++ b/packages/astro/src/core/config/settings.ts @@ -1,6 +1,6 @@ import path from 'node:path'; import { fileURLToPath, pathToFileURL } from 'node:url'; -import * as yaml from 'js-yaml'; +import { parseYaml } from '@astrojs/internal-helpers/yaml'; import * as toml from 'smol-toml'; import { getContentPaths } from '../../content/index.js'; import createPreferences from '../../preferences/index.js'; @@ -12,9 +12,9 @@ import { SUPPORTED_MARKDOWN_FILE_EXTENSIONS } from './../constants.js'; import { AstroError, AstroErrorData } from '../errors/index.js'; import { formatTOMLError, - formatYAMLException, + formatYAMLParseError, isTOMLError, - isYAMLException, + isYAMLParseError, } from '../errors/utils.js'; import { AstroTimer } from './timer.js'; import { loadTSConfig } from './tsconfig.js'; @@ -84,7 +84,7 @@ export function createBaseSettings( extensions: ['.yaml', '.yml'], getEntryInfo({ contents, fileUrl }) { try { - const data = yaml.load(contents, { filename: fileURLToPath(fileUrl) }); + const data = parseYaml(contents); const rawData = contents; return { data, rawData }; @@ -93,8 +93,8 @@ export function createBaseSettings( fileURLToPath(contentDir), fileURLToPath(fileUrl), ); - const formattedError = isYAMLException(e) - ? formatYAMLException(e) + const formattedError = isYAMLParseError(e) + ? formatYAMLParseError(e) : new Error('contains invalid YAML.'); throw new AstroError({ diff --git a/packages/astro/src/core/errors/utils.ts b/packages/astro/src/core/errors/utils.ts index 1e0cd5c53b19..227277594742 100644 --- a/packages/astro/src/core/errors/utils.ts +++ b/packages/astro/src/core/errors/utils.ts @@ -1,8 +1,10 @@ -import type { YAMLException } from 'js-yaml'; +import type { YAMLParseError } from '@astrojs/internal-helpers/yaml'; import type { TomlError } from 'smol-toml'; import type { ErrorPayload as ViteErrorPayload } from 'vite'; import type { SSRError } from '../../types/public/internal.js'; +export { isYAMLParseError } from '@astrojs/internal-helpers/yaml-error'; + /** * Get the line and character based on the offset * @param offset The index of the position @@ -72,17 +74,19 @@ function getLineOffsets(text: string) { return lineOffsets; } -export function isYAMLException(err: unknown): err is YAMLException { - return err instanceof Error && err.name === 'YAMLException'; -} - /** Format YAML exceptions as Vite errors */ -export function formatYAMLException(e: YAMLException): ViteErrorPayload['err'] { +export function formatYAMLParseError(e: YAMLParseError): ViteErrorPayload['err'] { + const position = e.linePos?.[0]; + const loc = position + ? { + line: position.line, + column: position.col - 1, + } + : undefined; return { name: e.name, - id: e.mark.name, - loc: { file: e.mark.name, line: e.mark.line + 1, column: e.mark.column }, - message: e.reason, + loc, + message: e.message, stack: e.stack ?? '', }; } diff --git a/packages/integrations/markdoc/src/content-entry-type.ts b/packages/integrations/markdoc/src/content-entry-type.ts index 6b3b9d52e815..14b2fe259b40 100644 --- a/packages/integrations/markdoc/src/content-entry-type.ts +++ b/packages/integrations/markdoc/src/content-entry-type.ts @@ -2,6 +2,7 @@ import fs from 'node:fs'; import path from 'node:path'; import { fileURLToPath, pathToFileURL } from 'node:url'; import { parseFrontmatter } from '@astrojs/internal-helpers/frontmatter'; +import { isYAMLParseError } from '@astrojs/internal-helpers/yaml-error'; import type { Config as MarkdocConfig, Node } from '@markdoc/markdoc'; import Markdoc from '@markdoc/markdoc'; import type { AstroConfig, ContentEntryType } from 'astro'; @@ -414,11 +415,15 @@ function safeParseFrontmatter(fileContents: string, filePath: string) { // because markdoc struggles with spaces return parseFrontmatter(fileContents, { frontmatter: 'empty-with-lines' }); } catch (e: any) { - if (e.name === 'YAMLException') { - const err: Error & ViteErrorPayload['err'] = e; + if (isYAMLParseError(e)) { + const err = e as Error & ViteErrorPayload['err']; + err.stack ??= ''; err.id = filePath; - err.loc = { file: e.id, line: e.mark.line + 1, column: e.mark.column }; - err.message = e.reason; + const position = e.linePos?.[0]; + if (position) { + err.loc = { file: filePath, line: position.line, column: position.col - 1 }; + } + err.message = e.message; throw err; } else { throw e; diff --git a/packages/integrations/mdx/src/utils.ts b/packages/integrations/mdx/src/utils.ts index 011d13060ae9..c8313d27c88a 100644 --- a/packages/integrations/mdx/src/utils.ts +++ b/packages/integrations/mdx/src/utils.ts @@ -1,4 +1,5 @@ import { parseFrontmatter } from '@astrojs/internal-helpers/frontmatter'; +import { isYAMLParseError } from '@astrojs/internal-helpers/yaml-error'; import type { AstroConfig, SSRError } from 'astro'; export function appendForwardSlash(path: string) { @@ -47,11 +48,15 @@ export function safeParseFrontmatter(code: string, id: string) { try { return parseFrontmatter(code, { frontmatter: 'empty-with-spaces' }); } catch (e: any) { - if (e.name === 'YAMLException') { - const err: SSRError = e; + if (isYAMLParseError(e)) { + const err = e as SSRError; + err.stack ??= ''; err.id = id; - err.loc = { file: e.id, line: e.mark.line + 1, column: e.mark.column }; - err.message = e.reason; + const position = e.linePos?.[0]; + if (position) { + err.loc = { file: id, line: position.line, column: position.col - 1 }; + } + err.message = e.message; throw err; } else { throw e; diff --git a/packages/internal-helpers/package.json b/packages/internal-helpers/package.json index b923115aff1c..01e5ea32a8e7 100644 --- a/packages/internal-helpers/package.json +++ b/packages/internal-helpers/package.json @@ -22,7 +22,9 @@ "./markdown": "./dist/markdown.js", "./mdx": "./dist/mdx.js", "./frontmatter": "./dist/frontmatter.js", - "./shiki": "./dist/shiki.js" + "./shiki": "./dist/shiki.js", + "./yaml": "./dist/yaml.js", + "./yaml-error": "./dist/yaml-error.js" }, "imports": { "#shiki-engine": { @@ -43,7 +45,7 @@ "dependencies": { "@types/hast": "^3.0.4", "@types/mdast": "^4.0.4", - "js-yaml": "^4.3.2", + "yaml": "^2.9.1", "picomatch": "^4.0.4", "retext-smartypants": "^6.2.0", "shiki": "^4.0.2", @@ -51,7 +53,6 @@ "unified": "^11.0.5" }, "devDependencies": { - "@types/js-yaml": "^4.0.9", "@types/picomatch": "^4.0.2", "astro-scripts": "workspace:*" }, diff --git a/packages/internal-helpers/src/frontmatter.ts b/packages/internal-helpers/src/frontmatter.ts index c6adfd514e5d..8e5592aee836 100644 --- a/packages/internal-helpers/src/frontmatter.ts +++ b/packages/internal-helpers/src/frontmatter.ts @@ -1,5 +1,5 @@ -import * as yaml from 'js-yaml'; import * as toml from 'smol-toml'; +import { parseYaml } from './yaml.js'; export function isFrontmatterValid(frontmatter: Record) { try { @@ -22,7 +22,7 @@ export function extractFrontmatter(code: string): string | undefined { } function getFrontmatterParser(code: string): [string, (str: string) => unknown] { - return frontmatterTypeRE.exec(code)?.[1] === '+++' ? ['+++', toml.parse] : ['---', yaml.load]; + return frontmatterTypeRE.exec(code)?.[1] === '+++' ? ['+++', toml.parse] : ['---', parseYaml]; } export interface ParseFrontmatterOptions { diff --git a/packages/internal-helpers/src/yaml-error.ts b/packages/internal-helpers/src/yaml-error.ts new file mode 100644 index 000000000000..2f2b6d91132c --- /dev/null +++ b/packages/internal-helpers/src/yaml-error.ts @@ -0,0 +1,5 @@ +import type { YAMLParseError } from 'yaml'; + +export function isYAMLParseError(err: unknown): err is YAMLParseError { + return err instanceof Error && err.name === 'YAMLParseError'; +} diff --git a/packages/internal-helpers/src/yaml.ts b/packages/internal-helpers/src/yaml.ts new file mode 100644 index 000000000000..bbc7b7ba507c --- /dev/null +++ b/packages/internal-helpers/src/yaml.ts @@ -0,0 +1,18 @@ +import { parseDocument, YAMLParseError } from 'yaml'; + +export function parseYaml(source: string): unknown { + const document = parseDocument(source, { + customTags: ['timestamp'], + merge: true, + schema: 'core', + }); + if (document.errors.length > 0) { + throw document.errors[0]; + } + if (document.contents === null) { + return undefined; + } + return document.toJS(); +} + +export { YAMLParseError }; diff --git a/packages/internal-helpers/test/frontmatter.test.ts b/packages/internal-helpers/test/frontmatter.test.ts new file mode 100644 index 000000000000..598e73c97177 --- /dev/null +++ b/packages/internal-helpers/test/frontmatter.test.ts @@ -0,0 +1,89 @@ +import assert from 'node:assert/strict'; +import { describe, it } from 'node:test'; +import { parseFrontmatter } from '../dist/frontmatter.js'; +import { parseYaml } from '../dist/yaml.js'; +import { isYAMLParseError } from '../dist/yaml-error.js'; + +describe('parseFrontmatter', () => { + it('parses YAML frontmatter', () => { + const { frontmatter, content } = parseFrontmatter('---\ntitle: Hello\n---\nBody'); + assert.deepEqual(frontmatter, { title: 'Hello' }); + assert.equal(content, '\nBody'); + }); + + it('parses unquoted dates as Date objects', () => { + const { frontmatter } = parseFrontmatter('---\ndate: 2022-01-01\n---\n'); + assert.ok(frontmatter.date instanceof Date); + assert.equal(frontmatter.date.toISOString(), '2022-01-01T00:00:00.000Z'); + }); + + it('keeps quoted dates as strings', () => { + const { frontmatter } = parseFrontmatter("---\ndate: '2022-01-01'\n---\n"); + assert.equal(frontmatter.date, '2022-01-01'); + }); + + it('resolves merge keys', () => { + const { frontmatter } = parseFrontmatter( + '---\ndefaults: &defaults\n layout: base\npage:\n <<: *defaults\n title: Hello\n---\n', + ); + assert.deepEqual(frontmatter.page, { layout: 'base', title: 'Hello' }); + }); + + it('returns an empty object for an empty frontmatter block', () => { + const { frontmatter, content } = parseFrontmatter('---\n---\nBody'); + assert.deepEqual(frontmatter, {}); + assert.equal(content, '\nBody'); + }); + + it('returns an empty object for a whitespace-only frontmatter block', () => { + const { frontmatter } = parseFrontmatter('---\n \n\n---\nBody'); + assert.deepEqual(frontmatter, {}); + }); + + it('returns an empty object for a comment-only frontmatter block', () => { + const { frontmatter } = parseFrontmatter('---\n# just a comment\n---\nBody'); + assert.deepEqual(frontmatter, {}); + }); + + it('returns an empty object when there is no frontmatter', () => { + const { frontmatter, content } = parseFrontmatter('Body'); + assert.deepEqual(frontmatter, {}); + assert.equal(content, 'Body'); + }); + + it('parses TOML frontmatter', () => { + const { frontmatter } = parseFrontmatter('+++\ntitle = "Hello"\n+++\nBody'); + assert.deepEqual(frontmatter, { title: 'Hello' }); + }); +}); + +describe('parseYaml', () => { + it('parses a YAML document', () => { + assert.deepEqual(parseYaml('a: 1\nb: true'), { a: 1, b: true }); + }); + + it('parses unquoted dates as Date objects', () => { + const data = parseYaml('date: 2022-01-01') as Record; + assert.ok(data.date instanceof Date); + }); + + it('returns undefined for empty input', () => { + assert.equal(parseYaml(''), undefined); + }); + + it('returns undefined for comment-only input', () => { + assert.equal(parseYaml('# just a comment'), undefined); + }); + + it('throws on multi-document input', () => { + assert.throws(() => parseYaml('a: 1\n---\nb: 2'), /multiple documents/); + }); + + it('identifies YAML parse errors', () => { + assert.equal(isYAMLParseError(new Error('invalid YAML')), false); + assert.throws( + () => parseYaml('a: ['), + (error) => isYAMLParseError(error), + ); + }); +}); diff --git a/packages/language-tools/vscode/package.json b/packages/language-tools/vscode/package.json index 7c536725451d..04809eed7a6e 100644 --- a/packages/language-tools/vscode/package.json +++ b/packages/language-tools/vscode/package.json @@ -247,7 +247,6 @@ "devDependencies": { "@astrojs/language-server": "^2.17.0", "@astrojs/ts-plugin": "^1.10.12", - "@types/js-yaml": "^4.0.9", "@types/mocha": "^10.0.10", "@types/node": "^22.10.6", "@types/vscode": "^1.101.0", @@ -258,7 +257,7 @@ "@vscode/vsce": "^3.9.2", "esbuild": "^0.17.19", "esbuild-plugin-copy": "^2.1.1", - "js-yaml": "^4.3.2", + "yaml": "^2.9.1", "kleur": "^4.1.5", "mocha": "^12.0.2", "ovsx": "^1.2.0", diff --git a/packages/language-tools/vscode/scripts/build-grammar.mjs b/packages/language-tools/vscode/scripts/build-grammar.mjs index ac385cbedb0a..9a1b5271a1ef 100644 --- a/packages/language-tools/vscode/scripts/build-grammar.mjs +++ b/packages/language-tools/vscode/scripts/build-grammar.mjs @@ -1,7 +1,7 @@ // @ts-check import fs from 'node:fs'; import { fileURLToPath } from 'node:url'; -import * as yaml from 'js-yaml'; +import { parse } from 'yaml'; import { dim, green } from 'kleur/colors'; const dt = new Intl.DateTimeFormat('en-us', { @@ -36,7 +36,7 @@ for (const grammarFile of grammarFiles) { * @param {URL} grammarFile */ function buildGrammar(grammarFile) { - const grammar = yaml.load(fs.readFileSync(grammarFile, 'utf8')); + const grammar = parse(fs.readFileSync(grammarFile, 'utf8')); const finalPath = fileURLToPath(grammarFile).replace('.src.yaml', '.json'); fs.writeFileSync(finalPath, JSON.stringify(grammar, null, 2)); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c55c27366ff8..59bb59b632bc 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -606,9 +606,6 @@ importers: http-cache-semantics: specifier: ^4.2.0 version: 4.2.0 - js-yaml: - specifier: ^4.3.2 - version: 4.3.2 jsonc-parser: specifier: ^3.3.1 version: 3.3.1 @@ -709,9 +706,6 @@ importers: '@types/http-cache-semantics': specifier: ^4.2.0 version: 4.2.0 - '@types/js-yaml': - specifier: ^4.0.9 - version: 4.0.9 '@types/parse-srcset': specifier: ^1.0.0 version: 1.0.0 @@ -6688,9 +6682,6 @@ importers: '@types/mdast': specifier: ^4.0.4 version: 4.0.4 - js-yaml: - specifier: ^4.3.2 - version: 4.3.2 picomatch: specifier: ^4.0.4 version: 4.0.7 @@ -6706,10 +6697,10 @@ importers: unified: specifier: ^11.0.5 version: 11.0.5 + yaml: + specifier: ^2.9.1 + version: 2.9.1 devDependencies: - '@types/js-yaml': - specifier: ^4.0.9 - version: 4.0.9 '@types/picomatch': specifier: ^4.0.2 version: 4.0.2 @@ -6941,9 +6932,6 @@ importers: '@astrojs/ts-plugin': specifier: ^1.10.12 version: link:../ts-plugin - '@types/js-yaml': - specifier: ^4.0.9 - version: 4.0.9 '@types/mocha': specifier: ^10.0.10 version: 10.0.10 @@ -6974,9 +6962,6 @@ importers: esbuild-plugin-copy: specifier: ^2.1.1 version: 2.1.1(esbuild@0.17.19) - js-yaml: - specifier: ^4.3.2 - version: 4.3.2 kleur: specifier: ^4.1.5 version: 4.1.5 @@ -6995,6 +6980,9 @@ importers: vscode-tmgrammar-test: specifier: ^0.1.3 version: 0.1.3 + yaml: + specifier: ^2.9.1 + version: 2.9.1 packages/language-tools/yaml2ts: dependencies: @@ -11069,9 +11057,6 @@ packages: '@types/istanbul-lib-coverage@2.0.6': resolution: {integrity: sha512-2QF/t/auWm0lsy8XtKVPG19v3sSOQlJe/YHZgfjb/KBBHOGSV+J2q/S671rcq9uTBrLAXmZpqJiaQbMT+zNU1w==} - '@types/js-yaml@4.0.9': - resolution: {integrity: sha512-k4MGaQl5TGo/iipqb2UDG2UwjXziSWkh0uysQelTlJpX1qGlpUZYm8PnO4DxG1qBomtJUdYJ6qR6xdIah10JLg==} - '@types/jsesc@2.5.1': resolution: {integrity: sha512-9VN+6yxLOPLOav+7PwjZbxiID2bVaeq0ED4qSQmdQTdjnXJSaCVKTR58t15oqH1H5t8Ng2ZX1SabJVoN9Q34bw==} @@ -13753,8 +13738,8 @@ packages: resolution: {integrity: sha512-SFNOvSJ+Dgf/9An904Yx+CgSlIPCkIpao4qo51lpee25TIRejdH3rhR4EZMGoNx3/TP3O+wzWuiTFl4sqbltzA==} hasBin: true - js-yaml@5.2.3: - resolution: {integrity: sha512-n+mUVyUX5bVv7G/G2zyIHOhdxfuU1dY2NOFzTQUWiMUbFss8b57NFlgCCaggU78wSw5KVS9cllzeLyzyR+n5nw==} + js-yaml@5.4.2: + resolution: {integrity: sha512-m+aqu+LwO1O6sIopafj8HUVl5aawITwZQe/yHpMCKjaWBaA/d07B/QdMb3529REftiU+RMMHL3Vlsw3hON7vWg==} hasBin: true jsdoc-type-pratt-parser@7.1.1: @@ -18860,7 +18845,7 @@ snapshots: '@modelcontextprotocol/client': 2.0.0 '@valibot/to-json-schema': 1.5.0(valibot@1.4.2) hono: 4.13.7 - js-yaml: 5.2.3 + js-yaml: 5.4.2 ulidx: 2.4.1 valibot: 1.4.2(typescript@6.0.3) transitivePeerDependencies: @@ -20997,8 +20982,6 @@ snapshots: '@types/istanbul-lib-coverage@2.0.6': {} - '@types/js-yaml@4.0.9': {} - '@types/jsesc@2.5.1': {} '@types/json-schema@7.0.15': {} @@ -24063,7 +24046,7 @@ snapshots: dependencies: argparse: 2.0.1 - js-yaml@5.2.3: + js-yaml@5.4.2: dependencies: argparse: 2.0.1 @@ -25116,7 +25099,7 @@ snapshots: glob: 13.0.6 is-path-inside: 4.0.0 is-unicode-supported: 0.1.0 - js-yaml: 5.2.3 + js-yaml: 5.4.2 minimatch: 10.2.6 ms: 2.1.3 picocolors: 1.1.1