Skip to content

Commit

Permalink
Add liquidDocBody to liquidHTMLParser
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmengo committed Dec 3, 2024
1 parent 1e24ef0 commit 84b7136
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 20 deletions.
10 changes: 6 additions & 4 deletions packages/liquid-html-parser/grammar/liquid-html.ohm
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,15 @@ Liquid <: Helpers {
commentBlockEnd
commentBlockStart = "{%" "-"? space* ("comment" endOfIdentifier) space* tagMarkup "-"? "%}"
commentBlockEnd = "{%" "-"? space* ("endcomment" endOfIdentifier) space* tagMarkup "-"? "%}"

liquidDoc =
liquidDocStart
anyExceptStar<(liquidDocStart|liquidDocEnd)>
liquidDocStart
liquidDocBody
liquidDocEnd
liquidDocStart = "{%" "-"? space* ("doc" endOfIdentifier) space* tagMarkup "-"? "%}"

liquidDocStart = "{%" "-"? space* ("doc" endOfIdentifier) space* tagMarkup "-"? "%}"
liquidDocEnd = "{%" "-"? space* ("enddoc" endOfIdentifier) space* tagMarkup "-"? "%}"
liquidDocBody = anyExceptStar<(liquidDocStart | liquidDocEnd)>

// In order for the grammar to "fallback" to the base case, this
// rule must pass if and only if we support what we parse. This
Expand Down
21 changes: 10 additions & 11 deletions packages/liquid-html-parser/src/stage-1-cst.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -980,11 +980,12 @@ describe('Unit: Stage 1 (CST)', () => {

it('should parse doc tags', () => {
for (const { toCST, expectPath } of testCases) {
const testStr = '{% doc -%} single line doc {%- enddoc %}';
const testStr = `{% doc -%} Renders loading-spinner. {%- enddoc %}`;

cst = toCST(testStr);
expectPath(cst, '0.type').to.equal('LiquidRawTag');
expectPath(cst, '0.name').to.equal('doc');
expectPath(cst, '0.body').to.equal(' single line doc ');
expectPath(cst, '0.body').to.include('Renders loading-spinner');
expectPath(cst, '0.whitespaceStart').to.equal('');
expectPath(cst, '0.whitespaceEnd').to.equal('-');
expectPath(cst, '0.delimiterWhitespaceStart').to.equal('-');
Expand All @@ -993,15 +994,13 @@ describe('Unit: Stage 1 (CST)', () => {
expectPath(cst, '0.blockStartLocEnd').to.equal(0 + '{% doc -%}'.length);
expectPath(cst, '0.blockEndLocStart').to.equal(testStr.length - '{%- enddoc %}'.length);
expectPath(cst, '0.blockEndLocEnd').to.equal(testStr.length);
expectPath(cst, '0.children').to.deep.equal([
{
locEnd: 26,
locStart: 11,
source: '{% doc -%} single line doc {%- enddoc %}',
type: 'TextNode',
value: 'single line doc',
},
]);
expectPath(cst, '0.children').to.deep.equal({
locEnd: 35,
locStart: 11,
source: '{% doc -%} Renders loading-spinner. {%- enddoc %}',
type: 'LiquidDocBody',
description: 'Renders loading-spinner.',
});
}
});

Expand Down
29 changes: 24 additions & 5 deletions packages/liquid-html-parser/src/stage-1-cst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export enum ConcreteNodeTypes {
RenderMarkup = 'RenderMarkup',
PaginateMarkup = 'PaginateMarkup',
RenderVariableExpression = 'RenderVariableExpression',
LiquidDocBody = 'LiquidDocBody',
}

export const LiquidLiteralValues = {
Expand Down Expand Up @@ -503,7 +504,13 @@ function toCST<T>(
source: string /* the original file */,
grammars: LiquidGrammars,
grammar: ohm.Grammar,
cstMappings: ('HelperMappings' | 'LiquidMappings' | 'LiquidHTMLMappings' | 'LiquidStatement')[],
cstMappings: (
| 'HelperMappings'
| 'LiquidMappings'
| 'LiquidHTMLMappings'
| 'LiquidStatement'
| 'LiquidDocMappings'
)[],
matchingSource: string = source /* for subtree parsing */,
offset: number = 0 /* for subtree parsing location offsets */,
): T {
Expand Down Expand Up @@ -630,13 +637,14 @@ function toCST<T>(
name: 'doc',
body: (tokens: Node[]) => tokens[1].sourceString,
children: (tokens: Node[]) => {
const contentNode = tokens[1];
return toCST(
source,
grammars,
TextNodeGrammar,
['HelperMappings'],
tokens[1].sourceString,
offset + tokens[1].source.startIdx,
grammars.Liquid,
['LiquidDocMappings'],
contentNode.sourceString,
offset + contentNode.source.startIdx,
);
},
whitespaceStart: (tokens: Node[]) => tokens[0].children[1].sourceString,
Expand Down Expand Up @@ -1099,6 +1107,16 @@ function toCST<T>(
},
};

const LiquidDocMappings: Mapping = {
Node: {
type: ConcreteNodeTypes.LiquidDocBody,
locStart,
locEnd,
source,
description: (tokens: Node[]) => tokens[0].sourceString,
},
};

const LiquidHTMLMappings: Mapping = {
Node(frontmatter: Node, nodes: Node) {
const self = this as any;
Expand Down Expand Up @@ -1254,6 +1272,7 @@ function toCST<T>(
LiquidMappings,
LiquidHTMLMappings,
LiquidStatement,
LiquidDocMappings,
};

const selectedMappings = cstMappings.reduce(
Expand Down

0 comments on commit 84b7136

Please sign in to comment.