Skip to content

Commit 1ec8c35

Browse files
authored
Refactor indented_data_reference to use Liquid internals (#16623)
* Refactor indented-data-reference * Add spacing around referenced reusables * Update expected test output * Fail silently with empty valuer
1 parent 35ebb6f commit 1ec8c35

File tree

4 files changed

+28
-26
lines changed

4 files changed

+28
-26
lines changed

content/actions/hosting-your-own-runners/managing-access-to-self-hosted-runners-using-groups.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ When creating a group, you must choose a policy that defines which repositories
4848
{% warning %}
4949

5050
**Warning**
51+
5152
{% indented_data_reference site.data.reusables.github-actions.self-hosted-runner-security spaces=3 %}
53+
5254
For more information, see "[About self-hosted runners](/actions/hosting-your-own-runners/about-self-hosted-runners#self-hosted-runner-security-with-public-repositories)."
5355

5456
{% endwarning %}
@@ -78,7 +80,9 @@ When creating a group, you must choose a policy that defines which organizations
7880
{% warning %}
7981

8082
**Warning**
83+
8184
{% indented_data_reference site.data.reusables.github-actions.self-hosted-runner-security spaces=3 %}
85+
8286
For more information, see "[About self-hosted runners](/actions/hosting-your-own-runners/about-self-hosted-runners#self-hosted-runner-security-with-public-repositories)."
8387

8488
{% endwarning %}

data/reusables/github-actions/self-hosted-runner-configure-runner-group-access.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
{% warning %}
66

77
**Warning**
8+
89
{% indented_data_reference site.data.reusables.github-actions.self-hosted-runner-security spaces=3 %}
10+
911
For more information, see "[About self-hosted runners](/actions/hosting-your-own-runners/about-self-hosted-runners#self-hosted-runner-security-with-public-repositories)."
1012

1113
{% endwarning %}
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
const Liquid = require('liquid')
2-
const liquid = new Liquid.Engine()
3-
const LiquidTag = require('./liquid-tag')
42
const assert = require('assert')
53

64
// This class supports a tag that expects two parameters, a data reference and `spaces=NUMBER`:
@@ -12,17 +10,14 @@ const assert = require('assert')
1210
// reference is used inside a block element (like a list or nested list) without
1311
// affecting the formatting when the reference is used elsewhere via {{ site.data.foo.bar }}.
1412

15-
module.exports = class IndentedDataReference extends LiquidTag {
16-
constructor (template, tagName, dataReferenceAndNumberOfSpaces) {
17-
super(template, tagName, dataReferenceAndNumberOfSpaces.trim())
18-
}
19-
20-
async parseTemplate (context) {
21-
const template = await this.getTemplate()
22-
13+
module.exports = class IndentedDataReference extends Liquid.Tag {
14+
async render (context) {
2315
// obfuscate first legit space, remove all other spaces, then restore legit space
2416
// this way we can support spaces=NUMBER as well as spaces = NUMBER
25-
const input = this.param.replace(/\s/, 'REALSPACE').replace(/\s/g, '').replace('REALSPACE', ' ')
17+
const input = this.markup.trim()
18+
.replace(/\s/, 'REALSPACE')
19+
.replace(/\s/g, '')
20+
.replace('REALSPACE', ' ')
2621

2722
const [dataReference, spaces] = input.split(' ')
2823

@@ -31,11 +26,16 @@ module.exports = class IndentedDataReference extends LiquidTag {
3126

3227
assert(parseInt(numSpaces) || numSpaces === '0', '"spaces=NUMBER" must include a number')
3328

34-
const renderedReference = await require('../render-content')(`{{ ${dataReference} }}`, context.environments[0])
29+
// Get the referenced value from the context
30+
const value = await context.get(dataReference)
31+
32+
// If nothing is found in the context, exit with nothing; this may
33+
// feel weird and that we should throw an error, but this is "The Liquid Way TM"
34+
if (!value) return
3535

3636
// add spaces to each line
37-
const renderedReferenceWithIndent = renderedReference.replace(/^/mg, ' '.repeat(numSpaces))
37+
const renderedReferenceWithIndent = value.replace(/^/mg, ' '.repeat(numSpaces))
3838

39-
return liquid.parseAndRender(template, { renderedReferenceWithIndent })
39+
return this.template.engine.parseAndRender(renderedReferenceWithIndent, context)
4040
}
4141
}

tests/unit/liquid-helpers.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,36 +85,32 @@ describe('liquid helper tags', () => {
8585

8686
test('without any number of spaces specified', async () => {
8787
const template = '{% indented_data_reference site.data.reusables.example %}'
88-
const expected = ` <p>a rose by any other name
89-
would smell as sweet</p>
90-
`
88+
const expected = ` a rose by any other name
89+
would smell as sweet`
9190
const output = await liquid.parseAndRender(template, context)
9291
expect(output).toBe(expected)
9392
})
9493

9594
test('with 0 spaces specified', async () => {
9695
const template = '{% indented_data_reference site.data.reusables.example spaces=0 %}'
97-
const expected = `<p>a rose by any other name
98-
would smell as sweet</p>
99-
`
96+
const expected = `a rose by any other name
97+
would smell as sweet`
10098
const output = await liquid.parseAndRender(template, context)
10199
expect(output).toBe(expected)
102100
})
103101

104102
test('with 0 spaces specified and whitespace around equals sign', async () => {
105103
const template = '{% indented_data_reference site.data.reusables.example spaces = 0 %}'
106-
const expected = `<p>a rose by any other name
107-
would smell as sweet</p>
108-
`
104+
const expected = `a rose by any other name
105+
would smell as sweet`
109106
const output = await liquid.parseAndRender(template, context)
110107
expect(output).toBe(expected)
111108
})
112109

113110
test('with 5 spaces specified', async () => {
114111
const template = '{% indented_data_reference site.data.reusables.example spaces=5 %}'
115-
const expected = ` <p>a rose by any other name
116-
would smell as sweet</p>
117-
`
112+
const expected = ` a rose by any other name
113+
would smell as sweet`
118114
const output = await liquid.parseAndRender(template, context)
119115
expect(output).toBe(expected)
120116
})

0 commit comments

Comments
 (0)