How to parse HTML string into HAST and vice versa #236
-
Hello Everyone! First of all, thanks for the amazing ecosystem. Second, I have been using this unified pipeline to transform and render the HTML string provided by the CMS. const content = unified()
.use(rehypeParse, { fragment: true })
.use(() => {}) // My transformations
.use(rehypeReact, {
Fragment: prod.Fragment,
jsx: prod.jsx,
jsxs: prod.jsxs,
createElement: production.jsx,
components: {
htoc: TableOfContent,
image: Image,
btl: BrokerTableLong,
bts: BrokerTableShort,
fai: renderIcon,
},
})
.processSync(data).result;
return <div className={styles["content"]}>{content}</div>; This process takes an HTML string and outputs I want to break this process into two steps:
Motivation: I am using nextjs for my project and I don't want to pass a large string of HTML data to the component as a porp. I instead want to parse it into an intermediate (light weight) form and pass that to the component to be rendered to reduce bandwidth and optimize performance I have read the documentation and tried multiple experiments but I always end up with:
Why can't I skip the compilation step in the first part and the parsing step in the second part? Is there is a function that should be used other than I have tried to use the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Welcome @Ahmedsaed! 👋 TL;DR do server side rendering through next and apply compression to the output.
It isn't really lighter weight versus rendering the HTML.
JSON isn't compression, gzip and brotli are compression To answer the XY question
See https://github.com/unifiedjs/unified?tab=readme-ov-file#overview
See https://github.com/unifiedjs/unified?tab=readme-ov-file#overview
See https://github.com/unifiedjs/unified?tab=readme-ov-file#overview To repeat though, you don't want to do this to achieve your end goal of more compact and efficient. |
Beta Was this translation helpful? Give feedback.
Welcome @Ahmedsaed! 👋
TL;DR do server side rendering through next and apply compression to the output.
It isn't really lighter weight versus rendering the HTML.
See the discussion at https://github.com/orgs/unifiedjs/discussions/188#discussioncomment-2466114
JSON isn't compression, gzip and brotli are compression
To answer the XY question
See https://github.com/unifiedjs/unified?tab=readme…