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
This commit is contained in:
Jason Etcovitch 2020-11-24 16:50:20 -05:00 коммит произвёл GitHub
Родитель 35ebb6f973
Коммит 1ec8c35e34
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 28 добавлений и 26 удалений

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

@ -48,7 +48,9 @@ When creating a group, you must choose a policy that defines which repositories
{% warning %}
**Warning**
{% indented_data_reference site.data.reusables.github-actions.self-hosted-runner-security spaces=3 %}
For more information, see "[About self-hosted runners](/actions/hosting-your-own-runners/about-self-hosted-runners#self-hosted-runner-security-with-public-repositories)."
{% endwarning %}
@ -78,7 +80,9 @@ When creating a group, you must choose a policy that defines which organizations
{% warning %}
**Warning**
{% indented_data_reference site.data.reusables.github-actions.self-hosted-runner-security spaces=3 %}
For more information, see "[About self-hosted runners](/actions/hosting-your-own-runners/about-self-hosted-runners#self-hosted-runner-security-with-public-repositories)."
{% endwarning %}

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

@ -5,7 +5,9 @@
{% warning %}
**Warning**
{% indented_data_reference site.data.reusables.github-actions.self-hosted-runner-security spaces=3 %}
For more information, see "[About self-hosted runners](/actions/hosting-your-own-runners/about-self-hosted-runners#self-hosted-runner-security-with-public-repositories)."
{% endwarning %}

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

@ -1,6 +1,4 @@
const Liquid = require('liquid')
const liquid = new Liquid.Engine()
const LiquidTag = require('./liquid-tag')
const assert = require('assert')
// This class supports a tag that expects two parameters, a data reference and `spaces=NUMBER`:
@ -12,17 +10,14 @@ const assert = require('assert')
// reference is used inside a block element (like a list or nested list) without
// affecting the formatting when the reference is used elsewhere via {{ site.data.foo.bar }}.
module.exports = class IndentedDataReference extends LiquidTag {
constructor (template, tagName, dataReferenceAndNumberOfSpaces) {
super(template, tagName, dataReferenceAndNumberOfSpaces.trim())
}
async parseTemplate (context) {
const template = await this.getTemplate()
module.exports = class IndentedDataReference extends Liquid.Tag {
async render (context) {
// obfuscate first legit space, remove all other spaces, then restore legit space
// this way we can support spaces=NUMBER as well as spaces = NUMBER
const input = this.param.replace(/\s/, 'REALSPACE').replace(/\s/g, '').replace('REALSPACE', ' ')
const input = this.markup.trim()
.replace(/\s/, 'REALSPACE')
.replace(/\s/g, '')
.replace('REALSPACE', ' ')
const [dataReference, spaces] = input.split(' ')
@ -31,11 +26,16 @@ module.exports = class IndentedDataReference extends LiquidTag {
assert(parseInt(numSpaces) || numSpaces === '0', '"spaces=NUMBER" must include a number')
const renderedReference = await require('../render-content')(`{{ ${dataReference} }}`, context.environments[0])
// Get the referenced value from the context
const value = await context.get(dataReference)
// 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
// add spaces to each line
const renderedReferenceWithIndent = renderedReference.replace(/^/mg, ' '.repeat(numSpaces))
const renderedReferenceWithIndent = value.replace(/^/mg, ' '.repeat(numSpaces))
return liquid.parseAndRender(template, { renderedReferenceWithIndent })
return this.template.engine.parseAndRender(renderedReferenceWithIndent, context)
}
}

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

@ -85,36 +85,32 @@ describe('liquid helper tags', () => {
test('without any number of spaces specified', async () => {
const template = '{% indented_data_reference site.data.reusables.example %}'
const expected = ` <p>a rose by any other name
would smell as sweet</p>
`
const expected = ` a rose by any other name
would smell as sweet`
const output = await liquid.parseAndRender(template, context)
expect(output).toBe(expected)
})
test('with 0 spaces specified', async () => {
const template = '{% indented_data_reference site.data.reusables.example spaces=0 %}'
const expected = `<p>a rose by any other name
would smell as sweet</p>
`
const expected = `a rose by any other name
would smell as sweet`
const output = await liquid.parseAndRender(template, context)
expect(output).toBe(expected)
})
test('with 0 spaces specified and whitespace around equals sign', async () => {
const template = '{% indented_data_reference site.data.reusables.example spaces = 0 %}'
const expected = `<p>a rose by any other name
would smell as sweet</p>
`
const expected = `a rose by any other name
would smell as sweet`
const output = await liquid.parseAndRender(template, context)
expect(output).toBe(expected)
})
test('with 5 spaces specified', async () => {
const template = '{% indented_data_reference site.data.reusables.example spaces=5 %}'
const expected = ` <p>a rose by any other name
would smell as sweet</p>
`
const expected = ` a rose by any other name
would smell as sweet`
const output = await liquid.parseAndRender(template, context)
expect(output).toBe(expected)
})