MCPcopy Index your code
hub / github.com/github/docs / getVersionBlocks

Function getVersionBlocks

script/helpers/get-version-blocks.js:5–57  ·  view source on GitHub ↗
(rawBlocks)

Source from the content-addressed store, hash-verified

3// Turn an array of Liquid conditional objects that results from ./get-liquid-conditionals.js into a more
4// detailed array of objects that includes GHES versioning information.
5export default function getVersionBlocks(rawBlocks) {
6 const versionBlocks = []
7
8 rawBlocks.forEach((block) => {
9 const condKeyword = block.conditional.includes('ifversion') ? 'ifversion' : 'elsif'
10 const condOnly = block.conditional.replace(/{%-? /, '').replace(/ -?%}/, '')
11 const condWithLiquid = block.conditional
12
13 // E.g., [ 'ghes', 'ghes < 2.21', 'ghae' ]
14 const condArgs = condOnly
15 .replace('ifversion ', '')
16 .replace('elsif ', '')
17 // Obfuscate with an arbitrary character we don't want to preserve, so we can split on that character.
18 // TODO: If preserving `or` and `and` turns out NOT to be important, we can split on those words instead.
19 .replaceAll(/ (or|and) /g, ' ~$1 ')
20 .split('~')
21 .map((arg) => arg.trim())
22
23 // E.g., [ 'ghes', '<', '2.21' ]
24 const ranges = condArgs
25 .map((arg) => arg.split(' '))
26 .filter(
27 (args) =>
28 args.some((arg) => supportedOperators.includes(arg)) && args.some((arg) => arg === 'ghes')
29 )
30 .map((args) => args.filter((arg) => !(arg === 'or' || (arg === 'and') | (arg === ''))))
31
32 // Remove the start tag and the end tag so we are left with the inner text.
33 // We don't need to do anything with this inner text other than check for nested conds.
34 let innerText = block.text.replace(condWithLiquid, '')
35 const indexOfLastEndif = innerText.lastIndexOf(' endif ')
36 innerText = innerText.slice(0, indexOfLastEndif)
37
38 // Remove any nested conditional content so we can check the top-level only.
39 const topLevelContent = innerText.replace(/{%-? ifversion[\S\s]+{%-? endif -?%}/g, '')
40
41 versionBlocks.push({
42 condKeyword,
43 condOnly,
44 condWithLiquid,
45 condArgs,
46 ranges,
47 content: block.text,
48 hasElsif: /{%-? elsif /.test(topLevelContent), // see if there is a nested elsif
49 hasElse: /{%-? else -?%}/.test(topLevelContent),
50 endIfText: block.endIfText,
51 startTagColumn1: block.positionStart[1] === 1,
52 endTagColumn1: block.positionEnd[1] === 1,
53 })
54 })
55
56 return versionBlocks
57}

Callers 1

removeLiquidStatementsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected