Skip to content

Commit 12fbc2f

Browse files
committed
parse string
1 parent ed32770 commit 12fbc2f

File tree

3 files changed

+86
-203
lines changed

3 files changed

+86
-203
lines changed

packages/code/src/Code.stories.tsx

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -34,55 +34,37 @@ const customActionButtons = [
3434
</IconButton>,
3535
];
3636

37-
// const jsSnippet = `
38-
// import datetime from './';
39-
// import shaneeza from './';
40-
41-
// const myVar = 42;
42-
43-
// var myObj = {
44-
// someProp: ['arr', 'ay'],
45-
// regex: /([A-Z])\w+/
46-
// }
47-
48-
// export default class myClass {
49-
// constructor(){
50-
// // access properties
51-
// this.myProp = false
52-
// }
53-
// }
37+
const jsSnippet = `
38+
import datetime from './';
5439
55-
// function greeting(entity) {
56-
// return \`Hello, \${entity}! Cras justo odio, dapibus ac {{facilisis}} in, egestas eget quam. Vestibulum id ligula porta felis euismod semper.\`;
57-
// }
40+
hello there _hi_ and ok this is _great_ yup
5841
59-
// console.log(greeting('World'));
60-
// `;
61-
62-
const jsSnippet = `
63-
/**
64-
* comment
65-
*/
66-
_shaneeza_
67-
import shaneeza from './';
68-
import {{ shaneeza }} from './';
69-
import {{ datetime }} from './';
42+
const myVar = 42;
7043
7144
var myObj = {
7245
someProp: ['arr', 'ay'],
7346
regex: /([A-Z])\w+/
7447
}
7548
76-
var myObj = {
77-
someProp: ['arr', 'ay']
49+
export default class myClass {
50+
constructor(){
51+
// access properties
52+
this.myProp = false
7853
}
54+
}
7955
80-
const check = { hello }
56+
function greeting(entity) {
57+
return \`Hello, \${entity}! Cras justo odio, dapibus ac {{facilisis}} in, egestas eget quam. Vestibulum id ligula porta felis euismod semper.\`;
58+
}
59+
60+
console.log(greeting('World'));
8161
`;
8262

83-
// const jsSnippet = `
84-
// This is a ((( line ))) that i want to highlight
85-
// `;
63+
const jsCustomSnippet = `
64+
This is a line where the word _highlight_ is considered a custom keyword and I can change the color with CSS.
65+
66+
This is another line with the word _password_ that I want to highlight.
67+
`;
8668

8769
// > 5 lines to trigger expandable code block
8870
const shortJsSnippet = `
@@ -174,6 +156,29 @@ LiveExample.parameters = {
174156
},
175157
};
176158

159+
export const CustomWord: StoryType<typeof Code, FontSizeProps> = ({
160+
baseFontSize,
161+
highlightLines,
162+
...args
163+
}: CodeProps & FontSizeProps) => (
164+
<LeafygreenProvider baseFontSize={baseFontSize}>
165+
<Code
166+
{...(args as CodeProps)}
167+
highlightLines={highlightLines ? [6, [10, 15]] : undefined}
168+
className={css`
169+
width: 100%;
170+
`}
171+
>
172+
{jsCustomSnippet}
173+
</Code>
174+
</LeafygreenProvider>
175+
);
176+
LiveExample.parameters = {
177+
chromatic: {
178+
disableSnapshot: true,
179+
},
180+
};
181+
177182
export const WithCustomActions: StoryType<typeof Code, FontSizeProps> = ({
178183
baseFontSize,
179184
highlightLines,

packages/code/src/Syntax/Syntax.tsx

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useMemo } from 'react';
2-
import { renderToString } from 'react-dom/server';
2+
// import { renderToString } from 'react-dom/server';
33
import { HLJSOptions, HLJSPlugin } from 'highlight.js';
44
import hljs from 'highlight.js/lib/core'; // Skip highlight's auto-registering
55
import hljsDefineGraphQL from 'highlightjs-graphql';
@@ -52,16 +52,6 @@ function initializeSyntaxHighlighting() {
5252
hljsDefineGraphQL(hljs);
5353
} else {
5454
hljs.registerLanguage(language, languageParsers[language]);
55-
56-
// const kwds = ['shaneeza'];
57-
58-
// hljs.getLanguage(language).keywords.custom = ['testing'];
59-
// const built_in = hljs.getLanguage(language);
60-
// console.log({ language, built_in });
61-
62-
// hljs.getLanguage(language).keywords.built_in = [
63-
// ...new Set([...built_in, ...kwds]),
64-
// ];
6555
}
6656
});
6757

@@ -71,23 +61,6 @@ function initializeSyntaxHighlighting() {
7161
} as Partial<HLJSOptions>);
7262

7363
hljs.addPlugin(renderingPlugin as HLJSPlugin);
74-
75-
// hljs.addPlugin({
76-
// 'before:highlight': results => {
77-
// console.log('🍓🍓🍓', { code: results.code });
78-
// results.code = results.code.replace(
79-
// /datetime/g,
80-
// '<div class="highlighted">datetime</div>',
81-
// );
82-
// },
83-
// });
84-
85-
// in after:highlight, the code is already a react component so I don't think I can modify it
86-
// hljs.addPlugin({
87-
// 'after:highlight': results => {
88-
// console.log('🍓🍓🍓', { results });
89-
// },
90-
// });
9164
}
9265

9366
const codeStyles = css`
@@ -133,12 +106,17 @@ function Syntax({
133106
highlightedContent.react
134107
);
135108

136-
const string = renderToString(content).replace(
137-
/_shaneeza_/g,
138-
'<span class="highlighted">shaneeza</span>',
139-
);
109+
/**
110+
* This is a workaround to add a custom class to any word wrapped in a underscore. E.g. _highlight_
111+
*/
112+
// const updatedStringWithCustomClass = renderToString(
113+
// content as ReactElement,
114+
// ).replace(
115+
// /_(\w+)_/g,
116+
// (_, word) => `<span class="lg-highlight-custom">${word}</span>`,
117+
// );
140118

141-
console.log({ beforeString: renderToString(content), string });
119+
// console.log({ beforeString: renderToString(content), updatedStringWithCustomClass });
142120

143121
const { theme, darkMode } = useDarkMode();
144122

@@ -174,8 +152,10 @@ function Syntax({
174152
border-spacing: 0;
175153
`}
176154
>
177-
{/* <tbody>{content}</tbody> */}
178-
<tbody dangerouslySetInnerHTML={{ __html: string }} />
155+
<tbody>{content}</tbody>
156+
{/* <tbody
157+
dangerouslySetInnerHTML={{ __html: updatedStringWithCustomClass }}
158+
/> */}
179159
</table>
180160
</code>
181161
</SyntaxContext.Provider>

0 commit comments

Comments
 (0)