Skip to content

Commit

Permalink
feat: export it from client instead of index
Browse files Browse the repository at this point in the history
  • Loading branch information
jpedroschmitz committed Dec 5, 2023
1 parent 786beef commit 40041f1
Show file tree
Hide file tree
Showing 5 changed files with 532 additions and 526 deletions.
4 changes: 3 additions & 1 deletion packages/html-to-slate-ast/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ npm install jsdom

### 2. Convert your data

If you are using Node.js, you will need to use the `htmlToSlateAST` function, which returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises). If you are using this package in the browser, you can use the `htmlToSlateASTSync` function, which is synchronous and doesn't require the `jsdom` package.
If you are using Node.js, you will need to use the `htmlToSlateAST` function, which returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises). If you are using this package in the browser, you can use the `htmlToSlateASTSync` function (available at `@graphcms/html-to-slate-ast/client`), which is synchronous and doesn't require `jsdom`.

```js
import { htmlToSlateAST } from '@graphcms/html-to-slate-ast';
// Or
import { htmlToSlateASTSync } from '@graphcms/html-to-slate-ast/client';

async function main() {
const htmlString = '<div><p>test</p></div>'; // or import from a file or database
Expand Down
23 changes: 23 additions & 0 deletions packages/html-to-slate-ast/src/client/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Descendant } from 'slate';
import { deserialize, normalizeHtml } from '../lib';

const parseDomDocumentSync = (normalizedHTML: string) => {
return new DOMParser().parseFromString(normalizedHTML, 'text/html');
};

export function htmlToSlateASTSync<T>(
html: string
): string | Descendant | ChildNode[] | Descendant[] | T | T[] {
if (
typeof window === 'undefined' ||
typeof window.DOMParser === 'undefined'
) {
throw new Error(
'This function is intended to be used in a browser environment only'
);
}

const normalizedHTML = normalizeHtml(html);
const domDocument = parseDomDocumentSync(normalizedHTML);
return deserialize(domDocument.body, window);
}
Loading

0 comments on commit 40041f1

Please # to comment.