Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rich Attribute Filters #888

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion mwdb/web/src/components/RichAttribute/MarkedMustache.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
renderTokens,
Token,
} from "@mwdb-web/commons/helpers";
import { JSONValue, filter, filters } from "./filters";

/**
* Markdown with Mustache templates for React
Expand All @@ -24,6 +25,8 @@ function appendToLastElement(array: string[], value: string) {
return [...array.slice(0, -1), array[array.length - 1] + value];
}

const availableFilters: string[] = filters.map((f: filter) => f.name);

function splitName(name: string) {
if (name === ".")
// Special case for "this"
Expand Down Expand Up @@ -103,7 +106,7 @@ class MustacheContext extends Mustache.Context {
return parentPath;
}

lookup(name: string) {
_lookup(name: string) {
let searchable = false;
// Check for searchable mark at the beginning
if (name[0] === "@") {
Expand Down Expand Up @@ -137,6 +140,25 @@ class MustacheContext extends Mustache.Context {
}
return currentObject;
}

lookup(name: string) {
const filter_expr = name.split("|");
const variable = filter_expr.shift()!.trim();
var original: JSONValue = this._lookup(variable);
if (!original) {
return undefined;
}

for (let filter of filter_expr) {
if (!(filter in availableFilters)) {
continue;
}
const action = filters.filter((f: filter) => f.name == filter)[0].action;
original = action(original);
}

return original;
}
}

// Extended Writer to escape Markdown characters instead of HTML
Expand Down
22 changes: 22 additions & 0 deletions mwdb/web/src/components/RichAttribute/filters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export type JSONValue =
| string
| number
| boolean
| null

export type filter = {
name: string,
action: (value: JSONValue) => JSONValue,
}

export const filters: filter[] = [
{
name: "upper",
action: (value: JSONValue) => {
if (typeof value === "string") {
return value.toUpperCase();
}
return value;
}
}
];
Loading