Skip to content

Commit 9563715

Browse files
authoredFeb 20, 2025··
Add padding for liquid doc content (#802)

File tree

6 files changed

+64
-11
lines changed

6 files changed

+64
-11
lines changed
 

‎.changeset/hungry-socks-report.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@shopify/prettier-plugin-liquid': patch
3+
---
4+
5+
Add padding for liquid doc content

‎.changeset/yellow-kids-bake.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@shopify/liquid-html-parser': patch
3+
---
4+
5+
[internal] Fix typo in liquid doc parser

‎packages/liquid-html-parser/src/stage-1-cst.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -1369,19 +1369,19 @@ function toLiquidDocAST(source: string, matchingSource: string, offset: number)
13691369
/**
13701370
* Reusable text node type
13711371
*/
1372-
const textNode = {
1372+
const textNode = () => ({
13731373
type: ConcreteNodeTypes.TextNode,
13741374
value: function () {
13751375
return (this as any).sourceString;
13761376
},
13771377
locStart,
13781378
locEnd,
13791379
source,
1380-
};
1380+
});
13811381

13821382
const LiquidDocMappings: Mapping = {
13831383
Node: 0,
1384-
TextNode: textNode,
1384+
TextNode: textNode(),
13851385
paramNode: {
13861386
type: ConcreteNodeTypes.LiquidDocParamNode,
13871387
name: 'param',
@@ -1400,9 +1400,9 @@ function toLiquidDocAST(source: string, matchingSource: string, offset: number)
14001400
source,
14011401
content: 2,
14021402
},
1403-
descriptionContent: textNode,
1403+
descriptionContent: textNode(),
14041404
paramType: 2,
1405-
paramTypeContent: textNode,
1405+
paramTypeContent: textNode(),
14061406
paramName: {
14071407
type: ConcreteNodeTypes.LiquidDocParamNameNode,
14081408
content: 0,
@@ -1419,7 +1419,7 @@ function toLiquidDocAST(source: string, matchingSource: string, offset: number)
14191419
source,
14201420
required: false,
14211421
},
1422-
paramDescription: textNode,
1422+
paramDescription: textNode(),
14231423
exampleNode: {
14241424
type: ConcreteNodeTypes.LiquidDocExampleNode,
14251425
name: 'example',
@@ -1428,9 +1428,9 @@ function toLiquidDocAST(source: string, matchingSource: string, offset: number)
14281428
source,
14291429
content: 2,
14301430
},
1431-
exampleContent: textNode,
1432-
textValue: textNode,
1433-
fallbackNode: textNode,
1431+
exampleContent: textNode(),
1432+
textValue: textNode(),
1433+
fallbackNode: textNode(),
14341434
};
14351435

14361436
return toAST(res, LiquidDocMappings);

‎packages/prettier-plugin-liquid/src/printer/print/liquid.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -504,8 +504,22 @@ export function printLiquidDoc(
504504
print: LiquidPrinter,
505505
_args: LiquidPrinterArgs,
506506
) {
507-
const body = path.map((p: any) => print(p), 'nodes');
508-
return [indent([hardline, join(hardline, body)]), hardline];
507+
const nodes = path.map((p: any) => print(p), 'nodes') as string[][];
508+
509+
if (nodes.length === 0) return [];
510+
511+
const lines = [nodes[0]] as (string[] | doc.builders.Concat)[];
512+
513+
for (let i = 1; i < nodes.length; i++) {
514+
lines.push(hardline);
515+
// If the tag name is different from the previous one, add an extra line break
516+
if (nodes[i - 1][0] !== nodes[i][0]) {
517+
lines.push(hardline);
518+
}
519+
lines.push(nodes[i]);
520+
}
521+
522+
return [indent([hardline, lines]), hardline];
509523
}
510524

511525
export function printLiquidDocParam(

‎packages/prettier-plugin-liquid/src/test/liquid-doc/fixed.liquid

+18
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ It should remove empty lines at the end of the example content
7777
It should respect example content with param and description
7878
{% doc %}
7979
@param paramName - param with description
80+
8081
@example
8182
This is a valid example
8283
{% enddoc %}
@@ -102,7 +103,24 @@ It should add a space between a description tag and content
102103
It should handle param, example, and description nodes
103104
{% doc %}
104105
@param {String} paramName - param with description
106+
105107
@example
106108
This is a valid example
109+
107110
@description This is a description
108111
{% enddoc %}
112+
113+
It should add padding between dissimilar nodes
114+
{% doc %}
115+
@param {String} param1 - param with description
116+
@param {String} param2 - param with description
117+
118+
@example
119+
This is a valid example
120+
@example
121+
This is another valid example
122+
123+
@description This is a description
124+
125+
@param {String} param3 - param with description
126+
{% enddoc %}

‎packages/prettier-plugin-liquid/src/test/liquid-doc/index.liquid

+11
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,14 @@ It should handle param, example, and description nodes
107107
@description This is a description
108108
{% enddoc %}
109109

110+
It should add padding between dissimilar nodes
111+
{% doc %}
112+
@param {String} param1 - param with description
113+
@param {String} param2 - param with description
114+
@example
115+
This is a valid example
116+
@example
117+
This is another valid example
118+
@description This is a description
119+
@param {String} param3 - param with description
120+
{% enddoc %}

0 commit comments

Comments
 (0)
Please sign in to comment.