Skip to content

Commit be3fdf7

Browse files
authored
Feature: Variables Fallback (#19)
* add way to cache chunks * add chunk fallback * add parsing of variable * rename variable to variables * remove unused variable
1 parent c7e6cf2 commit be3fdf7

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/EditmodeContext.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ import { createContext } from "react";
33
export const EditmodeContext = createContext({
44
branch: null,
55
projectId: null,
6-
defaultChunks: null,
6+
defaultChunks: [],
77
});

src/utils/renderChunk.jsx

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ export const renderChunk = (cnk, props) => {
88
: cnk.content;
99

1010
let chunk = { ...cnk, content: sanitizedContent };
11+
let tokens = chunk.content.match(/\{{(.*?)\}}/g);
12+
13+
let parsedChunk = chunk.content;
14+
15+
if (tokens !== null) {
16+
tokens.forEach(function(token) {
17+
parsedChunk = parsedChunk.replace(token, props.variables[token.substr(2, token.length-4)]);
18+
});
19+
}
20+
1121
switch (chunk.chunk_type) {
1222
case "single_line_text":
1323
case "long_text":
@@ -20,13 +30,13 @@ export const renderChunk = (cnk, props) => {
2030
key={chunk.identifier}
2131
{...props}
2232
>
23-
{chunk.content}
33+
{parsedChunk}
2434
</em-span>)
2535
: (<Text
2636
key={chunk.identifier}
2737
{...props}
2838
>
29-
{chunk.content}
39+
{parsedChunk}
3040
</Text>);
3141
case "rich_text":
3242
return Platform.OS === 'web'
@@ -37,7 +47,7 @@ export const renderChunk = (cnk, props) => {
3747
data-chunk-content-key={chunk.content_key}
3848
data-chunk-type="rich_text"
3949
key={chunk.identifier}
40-
dangerouslySetInnerHTML={{__html: chunk.content}}
50+
dangerouslySetInnerHTML={{__html: parsedChunk}}
4151
{...props}
4252
>
4353
</em-span>)
@@ -62,7 +72,7 @@ export const renderChunk = (cnk, props) => {
6272
/>);
6373
default:
6474
return Platform.OS === 'web'
65-
? <span {...props}>{chunk.content}</span>
66-
: <Text {...props}>{chunk.content}</Text>;
75+
? <span {...props}>{parsedChunk}</span>
76+
: <Text {...props}>{parsedChunk}</Text>;
6777
}
6878
};

0 commit comments

Comments
 (0)