Skip to content

Commit f57a1d9

Browse files
authored
Improve expression syntax documentation (#1112)
* docs: separate syntax from example in v8.json * docs: switch to param names in expression syntax doc * docs: simplify variadic expression syntax * docs: fix typos in expression docs * style: change backtick to single quote * refactor: create syntax to markdown function * refactor: rename syntax variants to overloads * update changelog
1 parent 1bbd8ce commit f57a1d9

File tree

3 files changed

+1497
-513
lines changed

3 files changed

+1497
-513
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## main
22

33
### ✨ Features and improvements
4+
- Restructure expression syntax documentation ([#1112](https://github.com/maplibre/maplibre-style-spec/pull/1112))
45
- _...Add new stuff here..._
56

67
### 🐞 Bug fixes

build/generate-docs.ts

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@ import jsonStringify from 'json-stringify-pretty-compact';
99

1010
const BASE_PATH = 'docs';
1111

12+
type JsonExpressionSyntax = {
13+
overloads: {
14+
parameters: string[];
15+
'output-type': string;
16+
}[];
17+
parameters?: {
18+
name: string;
19+
type?: string;
20+
description?: string;
21+
}[];
22+
}
23+
1224
type JsonSdkSupport = {
1325
[info: string]: {
1426
js?: string;
@@ -118,7 +130,32 @@ function sdkSupportToMarkdown(support: JsonSdkSupport): string {
118130
markdown += `|${row}|${supportCell(supportMatrix.js)}|${supportCell(supportMatrix.android)}|${supportCell(supportMatrix.ios)}|\n`;
119131
}
120132
return markdown;
133+
}
121134

135+
/**
136+
* Converts the expression syntax object to markdown format.
137+
* @param key - the expression name
138+
* @param syntax - the expression syntax object in the style spec
139+
* @returns the markdown string for the expression's syntax section
140+
*/
141+
function expressionSyntaxToMarkdown(key: string, syntax: JsonExpressionSyntax) {
142+
let markdown = '\nSyntax:\n';
143+
const codeBlockLines = syntax.overloads.map((overload) => {
144+
return `[${[`"${key}"`, ...overload.parameters].join(', ')}]: ${overload['output-type']}`;
145+
});
146+
markdown += `${codeBlockMarkdown(codeBlockLines.join('\n'), 'js')}\n`;
147+
for (const parameter of syntax.parameters ?? []) {
148+
markdown += `- \`${parameter.name}\``;
149+
if (parameter.type) {
150+
const type = parameter.type.replaceAll('<', '&lt;').replaceAll('>', '&gt;');
151+
markdown += `: *${type}*`;
152+
}
153+
if (parameter.description) {
154+
markdown += ` — ${parameter.description}`;
155+
}
156+
markdown += '\n';
157+
}
158+
return markdown;
122159
}
123160

124161
/**
@@ -511,9 +548,8 @@ function createExpressionsContent() {
511548
}
512549
content += `\n### ${key}\n`;
513550
content += `${value.doc}\n`;
514-
value.example.syntax.method.unshift(`"${key}"`);
515-
content += `\nSyntax:\n${codeBlockMarkdown(`[${value.example.syntax.method.join(', ')}]: ${value.example.syntax.result}`, 'js')}\n`;
516-
content += `\nExample:\n${codeBlockMarkdown(`"some-property": ${formatJSON(value.example.value)}`)}\n`;
551+
content += expressionSyntaxToMarkdown(key, value.syntax);
552+
content += `\nExample:\n${codeBlockMarkdown(`"some-property": ${formatJSON(value.example)}`)}\n`;
517553
content += sdkSupportToMarkdown(value['sdk-support'] as any);
518554
content += '\n';
519555
}

0 commit comments

Comments
 (0)