Skip to content

Commit 4f0e978

Browse files
committed
feat: new funcs to see variables
New function and new data file from running the converter.
1 parent e17e6d1 commit 4f0e978

File tree

6 files changed

+1123
-72
lines changed

6 files changed

+1123
-72
lines changed

examples/autocomplete/index.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { getDirectives, Format, Directive } from '@nginx/reference-lib'
1+
import {
2+
getDirectives,
3+
Format,
4+
Directive,
5+
getVariables,
6+
} from '@nginx/reference-lib'
27
type autocomplete = {
38
/** name of the NGINX module */
49
m: string
@@ -10,10 +15,10 @@ type autocomplete = {
1015
* nginx config */
1116
v?: string
1217
/** markdown CSV for valid contexts */
13-
c: string
18+
c?: string
1419
/** markdown-formatted syntax specifications, including directive name.
1520
* Multiple syntaxes are seperated by newlines */
16-
s: string
21+
s?: string
1722
}
1823

1924
function toAutocomplete(d: Directive): autocomplete {
@@ -32,5 +37,10 @@ function toAutocomplete(d: Directive): autocomplete {
3237
return ret
3338
}
3439

35-
const formatted = getDirectives(Format.Markdown).map(toAutocomplete)
36-
console.log(JSON.stringify(formatted, undefined, 4))
40+
const directives = getDirectives(Format.Markdown).map(toAutocomplete)
41+
const variables = getVariables(Format.Markdown).map((v) => ({
42+
m: v.module,
43+
n: v.name,
44+
d: v.description,
45+
}))
46+
console.log(JSON.stringify(directives.concat(variables), undefined, 4))

reference-lib/index.ts

Lines changed: 60 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
import reference from './src/reference.json'
22

33
export interface Directive {
4-
name: string
5-
module: string
6-
description: string
7-
syntax: string[]
8-
contexts: string[]
9-
isBlock: boolean
10-
default: string
4+
name: string
5+
module: string
6+
description: string
7+
syntax: string[]
8+
contexts: string[]
9+
isBlock: boolean
10+
default: string
11+
}
12+
13+
export interface Variable {
14+
name: string
15+
module: string
16+
description: string
1117
}
1218

1319
export enum Format {
@@ -41,37 +47,63 @@ for (const modules of reference.modules) {
4147
* Returns all the nginx directives
4248
*
4349
* @param: format: format of the return type HTML or markdown
44-
*
4550
* @return: an array of Directives
4651
*/
47-
export function getDirectives(format=Format.HTML): Directive[] {
48-
const directives = reference.modules.flatMap((m) =>
49-
m.directives.map((d) => ({...d, module: m.name})))
50-
.map ((d) => ({
51-
name: d.name,
52-
module: d.module,
53-
description: format === Format.HTML ? d.description_html : d.description_md,
54-
syntax: format === Format.HTML ? d.syntax_html : d.syntax_md,
55-
contexts: d.contexts,
56-
isBlock: d.isBlock,
57-
default: d.default
58-
} as Directive))
59-
return directives
52+
export function getDirectives(format = Format.HTML): Directive[] {
53+
const directives = reference.modules
54+
.flatMap((m) => m.directives.map((d) => ({ ...d, module: m.name })))
55+
.map(
56+
(d) =>
57+
({
58+
name: d.name,
59+
module: d.module,
60+
description:
61+
format === Format.HTML ? d.description_html : d.description_md,
62+
syntax: format === Format.HTML ? d.syntax_html : d.syntax_md,
63+
contexts: d.contexts,
64+
isBlock: d.isBlock,
65+
default: d.default,
66+
} as Directive)
67+
)
68+
return directives
69+
}
70+
71+
/**
72+
* Returns all variables defined by any moduled
73+
*
74+
* @param: format: format of the return type HTML or markdown
75+
* @return: an array of Variables
76+
*/
77+
export function getVariables(format = Format.HTML): Variable[] {
78+
return reference.modules.flatMap(
79+
(m) =>
80+
m.variables?.map((v) => ({
81+
name: v.name,
82+
description:
83+
format === Format.HTML ? v.description_html : v.description_md,
84+
module: m.name,
85+
})) ?? []
86+
)
6087
}
6188

6289
/**
63-
* Returns the description corresponding to the directive name
90+
* Returns the description corresponding to the directive or variable name
6491
*
6592
* @param: directive: directive name to find
6693
* @param: module: optional name of module
6794
* @param: format: format of the return type HTML or markdown
6895
*
6996
* @return: a string containing the description of the directive in xml or markdown format
7097
*/
71-
export function find(directive: string, module: string | undefined, format=Format.HTML): string | undefined {
72-
const data =
73-
module
74-
? refDirectives.get(directive)?.find((d) => d.module.toUpperCase() === module.toUpperCase())
75-
: refDirectives.get(directive)?.at(0)
76-
return (format === Format.HTML ? data?.description_html : data?.description_md)
98+
export function find(
99+
directive: string,
100+
module: string | undefined,
101+
format = Format.HTML
102+
): string | undefined {
103+
const data = module
104+
? refDirectives
105+
.get(directive)
106+
?.find((d) => d.module.toUpperCase() === module.toUpperCase())
107+
: refDirectives.get(directive)?.at(0)
108+
return format === Format.HTML ? data?.description_html : data?.description_md
77109
}

reference-lib/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nginx/reference-lib",
3-
"version": "1.0.14",
3+
"version": "1.1.0",
44
"description": "",
55
"main": "dist/index.js",
66
"type": "module",
Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
1-
{"modules": [
1+
{
2+
"modules": [
23
{
34
"id": "/en/docs/http/ngx_http_access_module.html",
45
"name": "ngx_http_access_module",
56
"directives": [
67
{
78
"name": "allow",
89
"default": "",
9-
"contexts": [
10-
"http",
11-
"server",
12-
"location",
13-
"limit_except"
14-
],
15-
"syntax_md": [
16-
"*`address`* | *`CIDR`* | `unix:` | `all`"
17-
],
10+
"contexts": ["http", "server", "location", "limit_except"],
11+
"syntax_md": ["*`address`* | *`CIDR`* | `unix:` | `all`"],
1812
"syntax_html": [
1913
"<p><em><code>address</code></em> | <em><code>CIDR</code></em> | <code>unix:</code> | <code>all</code></p>\n"
2014
],
2115
"isBlock": false,
2216
"description_md": "Allows access for the specified network or address.",
2317
"description_html": "\u003cp\u003eAllows access for the specified network or address\u003c/p\u003e"
24-
}]
25-
}]
26-
}
18+
}
19+
],
20+
"variables": [
21+
{
22+
"name": "$gzip_ratio",
23+
"description_md": "achieved compression ratio, computed as the ratio between the\noriginal and compressed response sizes.",
24+
"description_html": "<p>achieved compression ratio, computed as the ratio between the\noriginal and compressed response sizes.</p>\n"
25+
}
26+
]
27+
}
28+
]
29+
}

0 commit comments

Comments
 (0)