| | import assert from 'assert' |
| |
|
| | import { THROW_ON_EMPTY, IndentedDataReferenceError } from './error-handling' |
| | import { getDataByLanguage } from '@/data-directory/lib/get-data' |
| |
|
| | |
| | interface LiquidTag { |
| | markup: string |
| | liquid: any |
| | parse(tagToken: any): void |
| | render(scope: any): Promise<string | undefined> |
| | } |
| |
|
| | interface LiquidScope { |
| | environments: { |
| | currentLanguage: string |
| | [key: string]: any |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | const IndentedDataReference: LiquidTag = { |
| | markup: '', |
| | liquid: null as any, |
| |
|
| | parse(tagToken: any): void { |
| | this.markup = tagToken.args.trim() |
| | }, |
| |
|
| | async render(scope: LiquidScope): Promise<string | undefined> { |
| | |
| | |
| | const input = this.markup |
| | .replace(/\s/, 'REALSPACE') |
| | .replace(/\s/g, '') |
| | .replace('REALSPACE', ' ') |
| |
|
| | const [dataReference, spaces] = input.split(' ') |
| |
|
| | |
| | const numSpaces: string = spaces ? spaces.replace(/spaces=/, '') : '2' |
| |
|
| | assert(parseInt(numSpaces) || numSpaces === '0', '"spaces=NUMBER" must include a number') |
| |
|
| | |
| | const text: string | undefined = getDataByLanguage( |
| | dataReference, |
| | scope.environments.currentLanguage, |
| | ) |
| | if (text === undefined) { |
| | if (scope.environments.currentLanguage === 'en') { |
| | const message = `Can't find the key 'indented_data_reference ${dataReference}' in the scope.` |
| | if (THROW_ON_EMPTY) { |
| | throw new IndentedDataReferenceError(message) |
| | } |
| | console.warn(message) |
| | } |
| | return |
| | } |
| |
|
| | |
| | const renderedReferenceWithIndent: string = text.replace(/^/gm, ' '.repeat(parseInt(numSpaces))) |
| |
|
| | return this.liquid.parseAndRender(renderedReferenceWithIndent, scope.environments) |
| | }, |
| | } |
| |
|
| | export default IndentedDataReference |
| |
|