Co-authored-by: Peter Bengtsson <peterbe@github.com>
This commit is contained in:
Sarah Schneider 2023-03-08 10:58:16 -05:00 коммит произвёл GitHub
Родитель d75be63157
Коммит 3b2f426296
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 24 добавлений и 19 удалений

Просмотреть файл

@ -1,11 +0,0 @@
import patterns from './patterns.js'
// This module searches a string for references to data objects
// It finds all references matching {{site.data.*}} and return an array of them
export default function getLiquidDataReferences(text) {
return (text.match(patterns.dataReference) || []).map((ref) => {
const cleaned = ref.replace(/\.+\//g, '').replace('{% data', '').replace('%}', '').trim()
return `site.data.${cleaned}`
})
}

Просмотреть файл

@ -19,7 +19,7 @@ export default {
const text = getDataByLanguage(this.path, scope.environments.currentLanguage)
if (text === undefined) {
if (scope.environments.currentLanguage === 'en') {
const message = `Can't find the key 'site.data.${this.path}' in the scope.`
const message = `Can't find the key 'data ${this.path}' in the scope.`
if (THROW_ON_EMPTY) {
throw new DataReferenceError(message)
}

Просмотреть файл

@ -37,7 +37,7 @@ export default {
const text = getDataByLanguage(dataReference, scope.environments.currentLanguage)
if (text === undefined) {
if (scope.environments.currentLanguage === 'en') {
const message = `Can't find the key 'site.data.${dataReference}' in the scope.`
const message = `Can't find the key 'indented_data_reference ${dataReference}' in the scope.`
if (THROW_ON_EMPTY) {
throw new IndentedDataReferenceError(message)
}

Просмотреть файл

@ -25,7 +25,8 @@ export const trailingSlash = /^(.+?)\/+?$/
export const searchPath = /\/search(?:\/)?(\?)/
export const ymd = /^\d{4}-\d{2}-\d{2}$/
export const hasLiquid = /[{{][{%]/
export const dataReference = /{% ?data\s(?:early-access\.)?(?:reusables|variables|ui)\..*?%}/gm
export const dataReference =
/{% ?(?:data|indented_data_reference)\s(?:early-access\.)?(?:reusables|variables|ui)\..*?%}/gm
export const imagePath = /\/?assets\/images\/.*?\.(png|svg|gif|pdf|ico|jpg|jpeg)/gi
export const homepagePath = /^\/\w{2}$/ // /en, /ja
export const multipleSlashes = /^(\/|\\){2,}/

Просмотреть файл

@ -6,7 +6,7 @@ import { isEqual, uniqWith } from 'lodash-es'
import { jest } from '@jest/globals'
import { loadPages } from '../../lib/page-data.js'
import getDataReferences from '../../lib/get-liquid-data-references.js'
import patterns from '../../lib/patterns.js'
import frontmatter from '../../lib/read-frontmatter.js'
import { getDataByLanguage, getDeepDataByLanguage } from '../../lib/get-data.js'
@ -14,6 +14,21 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url))
const pages = (await loadPages()).filter((page) => page.languageCode === 'en')
// Given syntax like {% data foo.bar %} or {% indented_data_reference foo.bar spaces=3 %},
// the following regex returns just the dotted path: foo.bar
// Note this regex allows nonstandard whitespace between terms; it does not enforce a single space.
// In other words, it will allow {%data foo.bar %} or {% data foo.bar %}.
// We should enforce a single space someday, but the content will need a lot of cleanup first, and
// we should have a more purpose-driven validation test for that instead of enforcing it here.
const getDataPathRegex =
/{%\s*?(?:data|indented_data_reference)\s+?(\S+?)\s*?(?:spaces=\d\d?\s*?)?%}/
const getDataReferences = (content) => {
const refs = content.match(patterns.dataReference) || []
return refs.map((ref) => ref.replace(getDataPathRegex, '$1'))
}
describe('data references', () => {
jest.setTimeout(60 * 1000)
@ -25,7 +40,7 @@ describe('data references', () => {
const file = path.join('content', page.relativePath)
const pageRefs = getDataReferences(page.markdown)
pageRefs.forEach((key) => {
const value = getDataByLanguage(key.replace('site.data.', ''), 'en')
const value = getDataByLanguage(key, 'en')
if (typeof value !== 'string') errors.push({ key, value, file })
})
})
@ -45,7 +60,7 @@ describe('data references', () => {
const { data: metadata } = frontmatter(fileContents, { filepath: page.fullPath })
const metadataRefs = getDataReferences(JSON.stringify(metadata))
metadataRefs.forEach((key) => {
const value = getDataByLanguage(key.replace('site.data.', ''), 'en')
const value = getDataByLanguage(key, 'en')
if (typeof value !== 'string') errors.push({ key, value, metadataFile })
})
})
@ -73,7 +88,7 @@ describe('data references', () => {
const reusableRefs = getDataReferences(JSON.stringify(reusablesPerFile))
reusableRefs.forEach((key) => {
const value = getDataByLanguage(key.replace('site.data.', ''), 'en')
const value = getDataByLanguage(key, 'en')
if (typeof value !== 'string') errors.push({ key, value, reusableFile })
})
})
@ -101,7 +116,7 @@ describe('data references', () => {
const variableRefs = getDataReferences(JSON.stringify(variablesPerFile))
variableRefs.forEach((key) => {
const value = getDataByLanguage(key.replace('site.data.', ''), 'en')
const value = getDataByLanguage(key, 'en')
if (typeof value !== 'string') errors.push({ key, value, variableFile })
})
})