Skip to content

Commit ed32770

Browse files
committed
wip
1 parent bb217e9 commit ed32770

File tree

3 files changed

+107
-52
lines changed

3 files changed

+107
-52
lines changed

packages/code/src/Code.stories.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,11 @@ const customActionButtons = [
6060
// `;
6161

6262
const jsSnippet = `
63-
/**
64-
* comment
63+
/**
64+
* comment
6565
*/
66-
import ***shaneeza*** from './';
66+
_shaneeza_
67+
import shaneeza from './';
6768
import {{ shaneeza }} from './';
6869
import {{ datetime }} from './';
6970
@@ -79,6 +80,10 @@ var myObj = {
7980
const check = { hello }
8081
`;
8182

83+
// const jsSnippet = `
84+
// This is a ((( line ))) that i want to highlight
85+
// `;
86+
8287
// > 5 lines to trigger expandable code block
8388
const shortJsSnippet = `
8489
function greeting(entity) {

packages/code/src/Syntax/Syntax.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { useMemo } from 'react';
2+
import { renderToString } from 'react-dom/server';
23
import { HLJSOptions, HLJSPlugin } from 'highlight.js';
34
import hljs from 'highlight.js/lib/core'; // Skip highlight's auto-registering
45
import hljsDefineGraphQL from 'highlightjs-graphql';
@@ -132,6 +133,13 @@ function Syntax({
132133
highlightedContent.react
133134
);
134135

136+
const string = renderToString(content).replace(
137+
/_shaneeza_/g,
138+
'<span class="highlighted">shaneeza</span>',
139+
);
140+
141+
console.log({ beforeString: renderToString(content), string });
142+
135143
const { theme, darkMode } = useDarkMode();
136144

137145
const baseFontSize = useBaseFontSize();
@@ -166,7 +174,8 @@ function Syntax({
166174
border-spacing: 0;
167175
`}
168176
>
169-
<tbody>{content}</tbody>
177+
{/* <tbody>{content}</tbody> */}
178+
<tbody dangerouslySetInnerHTML={{ __html: string }} />
170179
</table>
171180
</code>
172181
</SyntaxContext.Provider>

packages/code/src/renderingPlugin/renderingPlugin.tsx

Lines changed: 89 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,28 @@ function getHighlightedRowStyle(darkMode: boolean) {
158158
`;
159159
}
160160

161+
const mergeAsterisks = arr => {
162+
return arr.reduce((acc, curr, i, array) => {
163+
if (
164+
curr === '*' &&
165+
array[i + 1] === '*' &&
166+
array[i + 2] === '*' &&
167+
typeof array[i + 3] === 'string' &&
168+
array[i + 4] === '*' &&
169+
array[i + 5] === '*' &&
170+
array[i + 6] === '*'
171+
) {
172+
// Push the transformed string
173+
acc.push(`***${array[i + 3]}***`);
174+
// Skip the next 6 elements
175+
array.splice(i, 7);
176+
} else {
177+
acc.push(curr);
178+
}
179+
return acc;
180+
}, []);
181+
};
182+
161183
interface LineTableRowProps {
162184
lineNumber?: number;
163185
children: React.ReactNode;
@@ -247,7 +269,7 @@ export function flattenNestedTree(
247269
return function (
248270
entity: string | TokenObject,
249271
): string | FlatTokenObject | Array<string | FlatTokenObject> {
250-
console.log({ entity });
272+
// console.log({ entity });
251273
if (isString(entity)) {
252274
if (parentKinds.length > 0) {
253275
return {
@@ -258,52 +280,54 @@ export function flattenNestedTree(
258280
),
259281
children: [entity],
260282
};
261-
} else if (bracketArray.some(sub => entity.includes(sub))) {
262-
// The plugin returns {{ testing }} like this:
263-
// entity: "{"
264-
// entity: "{ "
265-
// entity: "testing }} "
266-
267-
if (entity.includes('{')) {
268-
// This is a bracket that has no space after it, meaning there is a string directly after it e.g. "{{"
269-
const singleBracket = '{';
270-
271-
// This is a bracket with a space after it and without a new line e.g. "{\n "
272-
const singleBracketWithSpaceAfter = '{ ';
273-
274-
// Check it the entity is a bracket with no space after it
275-
const isOnlySingleBracket = entity === singleBracket;
276-
277-
console.log({ entity, isOnlySingleBracket, lastStringEntity });
278-
279-
// If there is no space after it then remove it
280-
if (isOnlySingleBracket) {
281-
console.log({ entity });
282-
// save this entity as the last entity so that we can check if this directly follows another bracket
283-
lastStringEntity = entity;
284-
return '';
285-
}
286-
287-
// Check if the entity is a bracket with a space after it and if a empty bracket was the last entity before it e.g. "{{ "
288-
const isSingleBracketWithSpace =
289-
entity === singleBracketWithSpaceAfter &&
290-
lastStringEntity === singleBracket;
291-
292-
// If the entity is a bracket with a space after it and a single bracket was the last entity before it then remove it
293-
if (isSingleBracketWithSpace) {
294-
lastStringEntity = entity;
295-
return '';
296-
}
297-
}
283+
}
284+
// } else if (bracketArray.some(sub => entity.includes(sub))) {
285+
// // The plugin returns {{ testing }} like this:
286+
// // entity: "{"
287+
// // entity: "{ "
288+
// // entity: "testing }} "
289+
290+
// if (entity.includes('{')) {
291+
// // This is a bracket that has no space after it, meaning there is a string directly after it e.g. "{{"
292+
// const singleBracket = '{';
293+
294+
// // This is a bracket with a space after it and without a new line e.g. "{\n "
295+
// const singleBracketWithSpaceAfter = '{ ';
296+
297+
// // Check it the entity is a bracket with no space after it
298+
// const isOnlySingleBracket = entity === singleBracket;
299+
300+
// // console.log({ entity, isOnlySingleBracket, lastStringEntity });
301+
302+
// // If there is no space after it then remove it
303+
// if (isOnlySingleBracket) {
304+
// // console.log({ entity });
305+
// // save this entity as the last entity so that we can check if this directly follows another bracket
306+
// lastStringEntity = entity;
307+
// return '';
308+
// }
298309

299-
// If this entity has double brackets then remove them and add a special class to it e.g. 'testing }}'
300-
const cleanedEntity = entity.replace(' }}', '');
310+
// // Check if the entity is a bracket with a space after it and if a empty bracket was the last entity before it e.g. "{{ "
311+
// const isSingleBracketWithSpace =
312+
// entity === singleBracketWithSpaceAfter &&
313+
// lastStringEntity === singleBracket;
301314

302-
return {
303-
kind: generateKindClassName(`special ${prefix}special__${count++}`),
304-
children: [cleanedEntity],
305-
};
306-
} else {
315+
// // If the entity is a bracket with a space after it and a single bracket was the last entity before it then remove it
316+
// if (isSingleBracketWithSpace) {
317+
// lastStringEntity = entity;
318+
// return '';
319+
// }
320+
// }
321+
322+
// // If this entity has double brackets then remove them and add a special class to it e.g. 'testing }}'
323+
// const cleanedEntity = entity.replace(' }}', '');
324+
325+
// return {
326+
// kind: generateKindClassName(`special ${prefix}special__${count++}`),
327+
// children: [cleanedEntity],
328+
// };
329+
// }
330+
else {
307331
return entity;
308332
}
309333
// return parentKinds.length > 0
@@ -392,8 +416,10 @@ export function treeToLines(
392416
lines[currentLineIndex] = [];
393417
};
394418

419+
console.log('🤡🤡🤡🤡', { children });
420+
395421
flattenNestedTree(children).forEach(child => {
396-
console.log({ child });
422+
// console.log({ child });
397423

398424
// If the current element includes a line break, we need to handle it differently
399425
if (containsLineBreak(child)) {
@@ -427,9 +453,20 @@ export function treeToLines(
427453
}
428454
});
429455

430-
console.log('🤡🤡', { lines });
456+
// console.log('🤡🤡', { lines });
457+
458+
const mergedLines = lines.map(line => {
459+
const mergedLine = mergeAsterisks(line);
460+
461+
console.log({ line, mergedLine });
431462

432-
return lines;
463+
return mergedLine;
464+
});
465+
466+
// console.log('🌈🌈🌈🌈', { mergedLines });
467+
468+
// return lines;
469+
return mergedLines;
433470
}
434471

435472
interface TableContentProps {
@@ -507,11 +544,15 @@ export function TableContent({ lines }: TableContentProps) {
507544
}
508545

509546
const plugin: LeafyGreenHLJSPlugin = {
547+
'before:highlight': function (result: LeafyGreenHighlightResult) {
548+
console.log('🤡 before', { result });
549+
},
510550
'after:highlight': function (result: LeafyGreenHighlightResult) {
511551
// console.log('💚', { result });
512552

513553
const { rootNode } = result._emitter;
514554
// console.log(JSON.stringify(rootNode.children, null, 2));
555+
console.log({ rootNode });
515556
result.react = <TableContent lines={treeToLines(rootNode.children)} />;
516557

517558
// console.log('💚💚💚', { result: result.react });

0 commit comments

Comments
 (0)