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

ZETA-7333: add codeblock as welll #5

Merged
merged 1 commit into from
Dec 12, 2023
Merged
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
50 changes: 49 additions & 1 deletion src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {convertHtmlToADF} from '../html-to-adf';

it('works sir', () => {
it('should convert a simple paragraph with bold', () => {
const htmlString = `<p>test <b>this</b></p>`;
const result = convertHtmlToADF(htmlString);
const expected = {
Expand Down Expand Up @@ -28,5 +28,53 @@ it('works sir', () => {
}
]
}
console.log(result);
expect(result).toEqual(expected);
});

it('should convert a paragraph code block thereafter', () => {
const htmlString = `<p>test <b>this</b></p>
<code> // hello </code>
`;
const result = convertHtmlToADF(htmlString);
const expected = {
"version": 1,
"type": "doc",
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "test ",
marks: []
},
{
"type": "text",
"text": "this",
"marks": [
{
"type": "strong"
}
]
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": " // hello ",
"marks": [
{
"type": "code"
}
]
}
]
}
]
}
expect(result).toEqual(expected);
});
9 changes: 9 additions & 0 deletions src/html-to-adf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const processNode = (node: any, marks = [] as any) => {
marks.push({ type: "strong" });
} else if (node.name === "del") {
marks.push({ type: "strike" });
} else if (node.name === "code") {
marks.push({ type: "code" });
} else if (node.name === "em") {
marks.push({ type: "em" });
} else if (node.name === "u") {
Expand Down Expand Up @@ -56,13 +58,20 @@ export function convertHtmlToADF(htmlString: string): any {
} as any;

nodes.forEach((node: any) => {
console.log('node.name');
console.log(node.name);
const textNodes = processNode(node, []);
if (node.type === 'tag') {
if (node.name === "p") {
adf.content.push({
type: "paragraph",
content: textNodes,
});
} else if(node.name === "code") {
adf.content.push({
type: "paragraph",
content: textNodes,
});
} else if (node.name.startsWith("h")) {
const level = parseInt(node.name.substring(1));
adf.content.push({
Expand Down
Loading