Skip to content

Commit

Permalink
WIP: testing changes for debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
josephgregoryii committed Dec 3, 2021
1 parent 3dec7c4 commit d5f3183
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 52 deletions.
8 changes: 5 additions & 3 deletions scripts/actions/__tests__/deserialize-html.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import deserializeHTML from '../deserialize-html';
import serializeMDX from '../serialize-mdx';
import fs from 'fs';

test('deserializes DoNotTranslate', async () => {
test.only('deserializes DoNotTranslate', async () => {
const input = `
<DoNotTranslate>
<DoNotTranslate className="notranslate">
Not all who wander are lost...
</DoNotTranslate>
`;
Expand Down Expand Up @@ -43,7 +43,9 @@ This is an MDX doc
test('deserializes simple components', async () => {
const input = `
<Button to="/docs/agents/ruby-sdk">
Go to Ruby docs
<Button to="/joe/clark">
Go to Ruby docs
</Button>
</Button>
`;

Expand Down
2 changes: 2 additions & 0 deletions scripts/actions/utils/deserialization-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const deserializeComponent = (
node,
{ type, hasChildrenProp = true } = {}
) => {
console.log('top');
const { dataComponent, dataProps } = node.properties;
const name = dataComponent || node.tagName;
const props = dataProps ? deserializeJSValue(dataProps) : [];
Expand Down Expand Up @@ -97,6 +98,7 @@ const deserializeComponent = (
childrenNode && hasChildrenProp ? all(h, childrenNode) : []
);

console.log('deserialized node', newNode);
return newNode;
};

Expand Down
66 changes: 25 additions & 41 deletions scripts/actions/utils/handlers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const {
serializeComponent,
serializeJSValue,
serializeProps,
} = require('./serialization-helpers');
const {
deserializeComponent,
Expand Down Expand Up @@ -187,51 +188,34 @@ module.exports = {
serializeComponent(h, node, { textAttributes: ['title'] }),
},
DoNotTranslate: {
deserialize: (h, node) => {
// console.log('serialized node', node);
const { dataProps, dataComponent } = node.properties;
const children = deserializeJSValue(dataProps); // reusing code
const parsedChildren = JSON.parse(children);

// console.log('parsedChildren', parsedChildren);
deserialize: (h, node) => deserializeComponent(h, node),
// deserialize: (h, node) => {
// const { dataProps, dataComponent } = node.properties;
// const children = deserializeJSValue(dataProps); // reusing code

const newNode = h(
node,
'mdxBlockElement',
{ name: dataComponent, attributes: [] },
parsedChildren
);
// console.log('deserialized node', newNode.children);
// const newNode = h(
// node,
// 'mdxBlockElement',
// { name: dataComponent, attributes: [] },
// children
// );
// console.log('deserialized node', newNode);

return newNode;
},
// return newNode;
// },
serialize: (h, node) => {
// console.log('preserialized node', node);
const result = h(
node,
'div',
{
'data-type': 'component',
'data-component': 'DoNotTranslate',
'data-props': serializeJSValue(node.children), // reusing code
},

// TODO: configure position serialization (maybe?)
// line 87 of serialize-helpers.js contains this.
// This may be serializing the position, and why it turns
// out wierd
h(
node.position,
'div',
{
'data-type': 'prop',
'data-prop': 'children',
},
all(h, node)
)
);
return result;
serializeComponent(h, node);
},
// serialize: (h, node) => {
// console.log('preserialized node', node);
// const result = h(node, 'div', {
// 'data-type': 'component',
// 'data-component': 'DoNotTranslate',
// 'data-props': serializeProps(node),
// });
// console.log('serialized node', result);
// return result;
// },
},
thead: {
deserialize: deserializeComponent,
Expand Down
34 changes: 26 additions & 8 deletions scripts/actions/utils/serialization-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,33 @@ const serializeAttributeValue = (h, attribute) => {
const serializeTextProp = (h, node, propName) => {
const attribute = findAttribute(propName, node);

console.log('serailizeTextProp: ', attribute);

if (!attribute) {
return;
}

return h(node, 'div', { 'data-type': 'prop', 'data-prop': propName }, [
serializeAttributeValue(h, attribute),
]);
const result = h(
node,
'div',
{ 'data-type': 'prop', 'data-prop': propName },
[serializeAttributeValue(h, attribute)]
);

console.log('serailizeTextProp Result:', result);

return result;
};

const serializeJSValue = (value) =>
Buffer.from(JSON.stringify(value)).toString('base64');
const serializeJSValue = (value) => {
console.log('seralizeJSValue', value);
return Buffer.from(JSON.stringify(value)).toString('base64');
};

const serializeProps = (node) => {
console.log('serializeProps');
if (node.attributes.length === 0) {
console.log('serializeProps - no attributes');
return null;
}

Expand All @@ -76,7 +89,7 @@ const serializeComponent = (
node.children = children;
const inferredTagName = node.type === 'mdxSpanElement' ? 'span' : 'div';

return h(
const result = h(
node,
tagName || inferredTagName,
stripNulls({
Expand All @@ -98,10 +111,15 @@ const serializeComponent = (
)
.filter(Boolean)
);

console.log('serializeComponent result:', result);
return result;
};

const getComponentName = (node) =>
node.name === null ? 'React.Fragment' : node.name;
const getComponentName = (node) => {
console.log('getComponent:', node);
return node.name === null ? 'React.Fragment' : node.name;
};

const stripNulls = (obj) =>
Object.fromEntries(Object.entries(obj).filter(([, value]) => value != null));
Expand Down

0 comments on commit d5f3183

Please sign in to comment.