Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
50c0b2d
Removed duplicate actions reusable folder
sophietheking Feb 25, 2022
c9258a8
Merge branch 'main' into sophie-5758
sophietheking Feb 25, 2022
91f9349
Restoring a deleted organization on GHES (#25663)
lecoursen Feb 28, 2022
4707cd9
Merge branch 'main' into repo-sync
Octomerger Feb 28, 2022
7f898c3
Merge pull request #25709 from github/repo-sync
Octomerger Feb 28, 2022
9c8ab14
Set meta description on homepage (#25546)
rsese Feb 28, 2022
2ade7d8
reactify rest pages (#25005)
rachmari Feb 28, 2022
a297f2c
New translation batch for es (#25712)
docubot Feb 28, 2022
eef7705
throw if no value not found in indented_data_reference (#25711)
peterbe Feb 28, 2022
59388cb
Add About commit branches and tag labels
sophietheking Feb 24, 2022
e91372b
Adding screenshots
sophietheking Feb 24, 2022
9f2dda4
Add screenshot and clarify steps
sophietheking Feb 24, 2022
2707805
Optimize images
invalid-email-address Feb 25, 2022
675092a
Added link to information on tagging
sophietheking Feb 28, 2022
9dc1088
Adding steps to procedural section
sophietheking Feb 28, 2022
547a12c
Added a screenshot and a reusable
sophietheking Feb 28, 2022
409e7f8
Optimize images
invalid-email-address Feb 28, 2022
46a5416
Deleted Commit branch and tag labels page
sophietheking Feb 28, 2022
aa9ee8b
Removed reference to delete page
sophietheking Feb 28, 2022
bb1800a
Update content model to use `people` instead of `customers` (#25714)
ethanpalm Feb 28, 2022
bada144
lower rate limit in production much higher in dev and test (#25688)
peterbe Feb 28, 2022
ec09e7b
New translation batch for pt (#25708)
docubot Feb 28, 2022
dde4ae9
don't bother caching the lib/redirects/.redirects-cache_en_ja.json in…
peterbe Feb 28, 2022
a0844f6
New translation batch for cn (#25713)
docubot Feb 28, 2022
999e95e
New translation batch for ja (#25715)
docubot Feb 28, 2022
eee9933
Replace indented reusables
sophietheking Mar 1, 2022
1c75866
Merge branch 'main' into sophie-5758
sophietheking Mar 1, 2022
ccf02c1
Merge pull request #25657 from github/sophie-5758
sophietheking Mar 1, 2022
3ade48f
Update supported GitHub runners (#24186)
Steve-Glass Mar 1, 2022
6254af4
Merge branch 'main' into repo-sync
Octomerger Mar 1, 2022
80ace67
Merge pull request #25718 from github/repo-sync
Octomerger Mar 1, 2022
d11f852
CodeQL: Update SARIF uploading example (#25702)
RasmusWL Mar 1, 2022
9dff6e5
Update content/pull-requests/committing-changes-to-your-project/creat…
sophietheking Mar 1, 2022
88d5002
Merge branch 'main' into sophie-5306
sophietheking Mar 1, 2022
e81b8b3
Merge pull request #25603 from github/sophie-5306
sophietheking Mar 1, 2022
e69144b
temporarily re-introduct includes/rest_operations_at_current_path.htm…
peterbe Mar 1, 2022
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
Prev Previous commit
Next Next commit
throw if no value not found in indented_data_reference (#25711)
* throw if no value not found in indented_data_reference

* better comment

* deliberate writer-error saple

* all strings are truthy'ish

* undo

* fix tests
  • Loading branch information
peterbe authored Feb 28, 2022
commit eef77058ab919e2a0a486c8fe2b36e4d3adb679d
32 changes: 29 additions & 3 deletions lib/liquid-tags/indented-data-reference.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import assert from 'assert'

// If 'THROW_ON_EMPTY' is set and it's value is '0' or 'false' it becomes
// false. Or true if it's 'true' or '1'.
const THROW_ON_EMPTY = Boolean(
process.env.THROW_ON_EMPTY
? JSON.parse(process.env.THROW_ON_EMPTY)
: JSON.parse(process.env.CI || process.env.NODE_ENV !== 'production')
)

class IndentedDataReferenceError extends Error {}

// This class supports a tag that expects two parameters, a data reference and `spaces=NUMBER`:
//
// {% indented_data_reference foo.bar spaces=NUMBER %}
Expand Down Expand Up @@ -33,9 +43,25 @@ export default {
// Get the referenced value from the context
const value = await this.liquid.evalValue(`site.data.${dataReference}`, scope)

// If nothing is found in the context, exit with nothing; this may
// feel weird and that we should throw an error, but this is "The Liquid Way TM"
if (!value) return
// If value is falsy it can be because we completely failed to look
// it up. But it can also be literally an empty string.
// For example, the reusable could be entirely something like this:
//
// {% if some condition %}The meat{% endif %}
//
// Then it's working as expected. But if the reference is wrong, e.g.
//
// {% indented_data_reference reusables.foo.tyypu spaces=3 %}
//
// Or if the file simple doesn't exist, you get an undefined for the value.
if (typeof value !== 'string' && !value) {
const message = `Can't find the key 'site.data.${dataReference}' in the scope.`
if (THROW_ON_EMPTY) {
throw new IndentedDataReferenceError(message)
}
console.warn(message)
return
}

// add spaces to each line
const renderedReferenceWithIndent = value.replace(/^/gm, ' '.repeat(numSpaces))
Expand Down
17 changes: 10 additions & 7 deletions tests/content/site-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from 'path'
import fs from 'fs'
import { get, isPlainObject, has } from 'lodash-es'
import flat from 'flat'
import { ParseError } from 'liquidjs'
import loadSiteData from '../../lib/site-data.js'
import patterns from '../../lib/patterns.js'
import { liquid } from '../../lib/render-content/index.js'
Expand Down Expand Up @@ -63,20 +64,22 @@ describe('siteData module (English)', () => {
}
})

test('all Liquid templating is valid', async () => {
test('all Liquid tags are valid', async () => {
const dataMap = flat(data)
for (const key in dataMap) {
const value = dataMap[key]
if (!patterns.hasLiquid.test(value)) continue
let message = `${key} contains a malformed Liquid expression`
let result = null
try {
result = await liquid.parseAndRender(value)
await liquid.parseAndRender(value)
} catch (err) {
console.trace(err)
message += `: ${err.message}`
if (err instanceof ParseError) {
console.warn('value that failed to parse:', value)
throw new Error(`Unable to parse with Liquid: ${err.message}`)
}
// Note, the parseAndRender() might throw other errors. For
// example errors about the the data. But at least it
// managed to get paste the Liquid parsing phase.
}
expect(typeof result, message).toBe('string')
}
})

Expand Down