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

feat(core): expressive content replacement #139

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
5 changes: 5 additions & 0 deletions .changeset/tasty-pets-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@mod-protocol/core": minor
---

feat: expressive content replacement syntax via `mustache.js`
4 changes: 3 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@types/lodash.set": "^4.3.7",
"@types/lodash.tonumber": "^4.0.7",
"@types/lodash.tostring": "^4.1.7",
"@types/mustache": "^4.2.5",
"@types/react": "^18.2.0",
"@types/react-dom": "^18.2.0",
"eslint": "^7.32.0",
Expand All @@ -33,6 +34,7 @@
"lodash.mapvalues": "^4.6.0",
"lodash.set": "^4.3.2",
"lodash.tonumber": "^4.0.3",
"lodash.tostring": "^4.1.4"
"lodash.tostring": "^4.1.4",
"mustache": "^4.2.0"
}
}
19 changes: 18 additions & 1 deletion packages/core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ import {
HTTPAction,
} from "./manifest";
import { Embed } from "./embeds";
import Mustache from "mustache";

// Prevent default HTML escaping behaviour
Mustache.escape = function (value) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overriding globals is probably not a good idea given that this code is executed in the same context as other apps

return value;
};

export type ModElementRef<T> =
| {
Expand Down Expand Up @@ -315,7 +321,18 @@ export interface ExitActionResolver {
}

function replaceInlineContext(target: string, context: any): string {
return target.replace(/{{([^{{}}]+)}}/g, (_, key) => get(context, key, ``));
const output = Mustache.render(target, {
...context,
decimals: function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with this pattern, this list of functions is going to grow very long and it'll be bundled into every app.

return function (text: string, render: (text: string) => string) {
var parts = text.split(" ");
var value = parseFloat(render(parts[0]));
var decimalPlaces = parseInt(render(parts[1]), 10);
return value.toFixed(decimalPlaces);
};
},
});
return output;
}

function matchesOp(value: string, op: Op, context: any): boolean {
Expand Down
7 changes: 6 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4255,6 +4255,11 @@
resolved "https://registry.yarnpkg.com/@types/ms/-/ms-0.7.34.tgz#10964ba0dee6ac4cd462e2795b6bebd407303433"
integrity sha512-nG96G3Wp6acyAgJqGasjODb+acrI7KltPiRxzHPXnP3NgI28bpQDRv53olbqGXbfcgF5aiiHmO3xpwEpS5Ld9g==

"@types/mustache@^4.2.5":
version "4.2.5"
resolved "https://registry.yarnpkg.com/@types/mustache/-/mustache-4.2.5.tgz#9129f0d6857f976e00e171bbb3460e4b702f84ef"
integrity sha512-PLwiVvTBg59tGFL/8VpcGvqOu3L4OuveNvPi0EYbWchRdEVP++yRUXJPFl+CApKEq13017/4Nf7aQ5lTtHUNsA==

"@types/node@*", "@types/node@>=12.12.47", "@types/node@>=13.7.0", "@types/node@^20.8.8":
version "20.10.5"
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.10.5.tgz#47ad460b514096b7ed63a1dae26fad0914ed3ab2"
Expand Down Expand Up @@ -12137,7 +12142,7 @@ murmurhash3js-revisited@^3.0.0:
resolved "https://registry.yarnpkg.com/murmurhash3js-revisited/-/murmurhash3js-revisited-3.0.0.tgz#6bd36e25de8f73394222adc6e41fa3fac08a5869"
integrity sha512-/sF3ee6zvScXMb1XFJ8gDsSnY+X8PbOyjIuBhtgis10W2Jx4ZjIhikUCIF9c4gpJxVnQIsPAFrSwTCuAjicP6g==

mustache@^4.0.0:
mustache@^4.0.0, mustache@^4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/mustache/-/mustache-4.2.0.tgz#e5892324d60a12ec9c2a73359edca52972bf6f64"
integrity sha512-71ippSywq5Yb7/tVYyGbkBggbU8H3u5Rz56fH60jGFgr8uHwxs+aSKeqmluIVzM0m0kB7xQjKS6qPfd0b2ZoqQ==
Expand Down