diff --git a/packages/liquid-html-parser/src/stage-1-cst.ts b/packages/liquid-html-parser/src/stage-1-cst.ts index c701b3203..8aa1d281b 100644 --- a/packages/liquid-html-parser/src/stage-1-cst.ts +++ b/packages/liquid-html-parser/src/stage-1-cst.ts @@ -464,8 +464,6 @@ export type LiquidCST = LiquidConcreteNode[]; export type LiquidDocConcreteNode = ConcreteLiquidDocParamNode | ConcreteLiquidDocExampleNode; - - interface Mapping { [k: string]: number | TemplateMapping | TopLevelFunctionMapping; } diff --git a/packages/liquid-html-parser/src/stage-2-ast.spec.ts b/packages/liquid-html-parser/src/stage-2-ast.spec.ts index 0e382fe7f..c38bc0d18 100644 --- a/packages/liquid-html-parser/src/stage-2-ast.spec.ts +++ b/packages/liquid-html-parser/src/stage-2-ast.spec.ts @@ -1258,6 +1258,43 @@ describe('Unit: Stage 2 (AST)', () => { expectPath(ast, 'children.0.body.nodes.2.value').to.eql( '@unsupported this node falls back to a text node', ); + + ast = toLiquidAST(` + {% doc -%} + @example + This is a valid example + {%- enddoc %} + `); + expectPath(ast, 'children.0.type').to.eql('LiquidRawTag'); + expectPath(ast, 'children.0.name').to.eql('doc'); + expectPath(ast, 'children.0.body.nodes.0.name').to.eql('example'); + expectPath(ast, 'children.0.body.nodes.0.type').to.eql('LiquidDocExampleNode'); + expectPath(ast, 'children.0.body.nodes.0.exampleContent.type').to.eql('TextNode'); + expectPath(ast, 'children.0.body.nodes.0.exampleContent.value').to.eql( + 'This is a valid example', + ); + + ast = toLiquidAST(` + {% doc -%} + @example + This is a valid example + It can have multiple lines + @param {String} paramWithDescription - param with description + {% enddoc %} + `); + expectPath(ast, 'children.0.type').to.eql('LiquidRawTag'); + expectPath(ast, 'children.0.name').to.eql('doc'); + expectPath(ast, 'children.0.body.nodes.0.type').to.eql('LiquidDocExampleNode'); + expectPath(ast, 'children.0.body.nodes.0.name').to.eql('example'); + expectPath(ast, 'children.0.body.nodes.0.exampleContent.value').to.eql( + 'This is a valid example\nIt can have multiple lines', + ); + expectPath(ast, 'children.0.body.nodes.1.type').to.eql('LiquidDocParamNode'); + expectPath(ast, 'children.0.body.nodes.1.name').to.eql('param'); + expectPath(ast, 'children.0.body.nodes.1.paramName.value').to.eql('paramWithDescription'); + expectPath(ast, 'children.0.body.nodes.1.paramDescription.value').to.eql( + 'param with description', + ); }); it('should parse unclosed tables with assignments', () => { diff --git a/packages/liquid-html-parser/src/stage-2-ast.ts b/packages/liquid-html-parser/src/stage-2-ast.ts index 2d8b48d85..89d1de98d 100644 --- a/packages/liquid-html-parser/src/stage-2-ast.ts +++ b/packages/liquid-html-parser/src/stage-2-ast.ts @@ -1314,7 +1314,11 @@ function buildAst( source: node.source, exampleContent: { type: NodeTypes.TextNode, - value: node.exampleContent.value, + value: node.exampleContent.value + .split('\n') + .map((line) => line.trim()) + .filter(Boolean) + .join('\n'), position: position(node.exampleContent), source: node.exampleContent.source, }, diff --git a/packages/prettier-plugin-liquid/src/printer/print/liquid.ts b/packages/prettier-plugin-liquid/src/printer/print/liquid.ts index 54caad6f3..18db8e056 100644 --- a/packages/prettier-plugin-liquid/src/printer/print/liquid.ts +++ b/packages/prettier-plugin-liquid/src/printer/print/liquid.ts @@ -544,7 +544,12 @@ export function printLiquidDocExample( _args: LiquidPrinterArgs, ): Doc { const node = path.getValue(); - const parts: Doc[] = ['@example', 'asdf']; + const parts: Doc[] = ['@example']; + + if (node.exampleContent?.value.trim()) { + parts.push(hardline); + parts.push(join(hardline, node.exampleContent.value.split('\n'))); + } return parts; }