Skip to content
This repository was archived by the owner on Jul 30, 2025. It is now read-only.

Commit f4c8314

Browse files
committed
fix(plugins/plugin-client-common): adopt jq syntax for guidebook json filters
i.e. `.foo` instead of what we had before `foo`. This PR also allows for a deeper path project `.foo.bar`. This PR also allows exclusion after filtering, so that you can project out a subtree, and then exclude fields from that subtree.
1 parent 1f2aa6e commit f4c8314

File tree

1 file changed

+29
-18
lines changed
  • plugins/plugin-client-common/src/components/Content/Markdown/components/code

1 file changed

+29
-18
lines changed

plugins/plugin-client-common/src/components/Content/Markdown/components/code/filter.ts

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,39 +28,50 @@ function dump(field: any, language: string): string {
2828
}
2929
}
3030

31-
function filterIn(json: Record<string, any>, include: string, language: string) {
32-
return dump(json[include], language)
31+
function project(json: Record<string, any>, path: string[], idx = 0) {
32+
const field = json[path[idx]]
33+
if (idx === path.length - 1) {
34+
return field
35+
} else {
36+
return project(field, path, idx + 1)
37+
}
38+
}
39+
40+
function asFilter(filter: string | string[]): string[][] {
41+
return (typeof filter === 'string' ? [filter] : filter).map(filt => filt.split(/\./).slice(1))
3342
}
3443

35-
function filterOut(json: Record<string, any>, exclude: string[], language: string) {
36-
exclude.forEach(key => {
37-
delete json[key]
38-
})
39-
return dump(json, language)
44+
function filterIn(json: Record<string, any>, include: string[]) {
45+
return project(json, asFilter(include)[0])
4046
}
4147

42-
function asFilter(filter: string | string[]): string[] {
43-
if (typeof filter === 'string') {
44-
return [filter]
48+
function remove(json: Record<string, any>, path: string[], idx = 0) {
49+
if (idx === path.length - 1) {
50+
delete json[path[idx]]
4551
} else {
46-
return filter
52+
return remove(json[path[idx]], path, idx + 1)
4753
}
4854
}
4955

56+
function filterOut(json: Record<string, any>, exclude: string[][]) {
57+
exclude.forEach(path => remove(json, path))
58+
return json
59+
}
60+
5061
export default function bodyAndLanguage(body: string, language: string, attributes: Record<string, any>) {
5162
if (language === 'json' && (attributes.include || attributes.exclude || attributes.language)) {
5263
try {
5364
const json = JSON.parse(body)
5465

5566
const languageForView =
56-
attributes.language || (attributes.languageFrom && json[attributes.languageFrom]) || language
67+
attributes.language ||
68+
(attributes.languageFrom && project(json, asFilter(attributes.languageFrom)[0])) ||
69+
language
70+
71+
const json2 = attributes.include ? filterIn(json, attributes.include) : json
72+
const json3 = attributes.exclude ? filterOut(json2, asFilter(attributes.exclude)) : json2
5773

58-
const bodyForView =
59-
(attributes.include
60-
? filterIn(json, attributes.include, languageForView)
61-
: attributes.exclude
62-
? filterOut(json, asFilter(attributes.exclude), languageForView)
63-
: body) || ''
74+
const bodyForView = dump(json3, languageForView)
6475

6576
return { bodyForView, languageForView }
6677
} catch (err) {

0 commit comments

Comments
 (0)