https://codesandbox.io/s/quizzical-lake-16q4o?file=/pages/index.tsx
์ฌ์ฉ์ ์ง์ ๊ตฌ์ฑ ์์๋ก ๊ต์ฒดํ ๋ ํญ์ false๊ฐ ๋ฐํ๋ฉ๋๋ค.
ํ์ฌ any ์ ํ์ ์ฌ์ฉํ๊ณ ์์ต๋๋ค.
๊ทธ๋ฌ๋ ๋ชจ๋ ์ ํ ์ด์ธ์ ๋ค๋ฅธ ์๋ฃจ์
์ด ์์ต๋๊น?
๋ฌธ์ ๋ฅผ ์ด์ด์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค @purp1eeeeee
CodeSandbox ์์ ์์๋ React ์์๋ฅผ ๋ฐํํ์ง ์์ผ๋ฏ๋ก ๋ฐ๊พธ๊ธฐ๊ฐ ์๋ํ์ง ์์ต๋๋ค. ์ฝ์ด ๋ณด๊ธฐ ๋ฅผ ์ฐธ์กฐํ์ญ์์ค.
๊ทธ๋ ๋ค๋ฉด ๋ก๊ทธ๋ฅผ ์ฌ์ฉ์ ์ ์ ์์์ ๋ฐํ์ผ๋ก ๋์ฒดํ ์ ์์ต๋๊น?
export default function IndexPage() {
const html = "<h1>hoge</h1>";
const options: HTMLReactParserOptions = {
replace: (domNode) => {
- console.log(domNode);
- console.log(domNode instanceof Element);
+ if (domNode.name === "h1") {
+ return <h1>replaced</h1>;
+ }
}
};
return <div>{parse(html, options)}</div>;
}
์ ๋ฐ์ดํธ๋ CodeSandbox๋ฅผ ์ฐธ์กฐํ์ญ์์ค.
์ด ์ง์นจ์ ๋ฐ๋ฅด๊ณ ๋์ผํ ๋ฌธ์ ๊ฐ ์์ต๋๋ค. attribs
์์ domNode
attribs
์์ฑ์ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค.
typescript check domNode
๋ Element
์ ํ์ด ์๋๋ผ DOMNode
์ธ์คํด์ค์ด๋ฏ๋ก DOMNode
์ attribs
์๊ธฐ ๋๋ฌธ์ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค. ์ฌ์ฐ.
๊ทธ๋ฆฌ๊ณ ์ด ์กฐ๊ฑด์ผ๋ก console.log(domNode instanceof Element)
ํ๋ฉด domNode์ ์ธ์คํด์ค๊ฐ DOMNode
์ด๊ธฐ ๋๋ฌธ์ ํญ์ false๋ฅผ ๋ฐํํฉ๋๋ค.
์ด์ ๋ํ ํด๊ฒฐ์ฑ
์ด ์์ต๋๋ค. ๋์์ด ๋ ์ ์์ต๋๋ค. domNode ๋งค๊ฐ๋ณ์์ Element
์ ํ์ ์ ์ํ์ต๋๋ค.
import parse, { HTMLReactParserOptions } from "html-react-parser";
import { Element } from "domhandler/lib/node";
export function contentHandler(postContent: string) {
const options: HTMLReactParserOptions = {
replace: (domNode: Element) => {
if (domNode.attribs) {
if (domNode.attribs.id === 'shortcode') {
return <div className="leadform">Shortcode</div>;
}
}
},
};
return parse(postContent, options);
}
Next.js ํ๋ก์ ํธ์ ๋์ผํ ๋ฌธ์ ๊ฐ ์์์ ํ์ธํ ์ ์์ต๋๋ค. Typescript type-casting domNode
๋ html-react-parser๋ฅผ ์
๊ทธ๋ ์ด๋ํ ํ ์ ์ผํ ํด๊ฒฐ ๋ฐฉ๋ฒ์ธ ๊ฒ ๊ฐ์ต๋๋ค.
@kakaeriel
๋๋ฌด ์ข์! ๊ฐ์ฌํฉ๋๋ค๐
Typescript ํ๋ก์ ํธ์ ์ด๋ฌํ ๋ฌธ์ ๊ฐ ์์ต๋๋ค. @kakaeriel ์ฒ๋ผ Typescript์ ์น์ ์ ํฌํจํ์ฌ ๊ต์ฒดํ๋ ๋ฐฉ๋ฒ์ ๋ํ ์ด ์ง์นจ์ ๋ฐ๋ฅด๊ณ
@kakaeriel ์ ์๋ฃจ์
์ด ์ ์๊ฒ ํจ๊ณผ์ ์
๋๋ค. instanceof
์ฐ์ฐ์๋ ์๋ํ์ง ์์ผ๋ฉฐ(์์๋ ์ ํ์ด ์๋) ์ ํ ์ ์๋ฅผ ์ฐพ๋ node_modules๋ฅผ ๋ช ์๊ฐ ๋์ ํ์ํ ํ ์ด ํด๊ฒฐ ๋ฐฉ๋ฒ์ ์ฐพ๋ ๊ฒ์ด ์๋ช
์ ์์ธ์
๋๋ค.
@kakaeriel ๋์ฒด ์๋ฃจ์
์ ์ ๊ณตํด ์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค. README.md
๋ฌธ์๋ฅผ ๊ฐ์ ํ๊ธฐ ์ํด PR์ ์์ฑํ๋ ๋ฐ ๊ด์ฌ์ด ์์ผ์ญ๋๊น?
@remarkablemark ๋ค ๋ง์ต๋๋ค! ์ต๋ํ ๋นจ๋ฆฌ PR์ ์์ฑํ๊ฒ ์ต๋๋ค.
์ฐ์. NextJS๋ฅผ ์ฌ์ฉํ์ฌ ๋์ผํ ๋ฌธ์ ๊ฐ ์์ต๋๋ค.
typescript check
domNode
๋Element
์ ํ์ด ์๋๋ผDOMNode
์ธ์คํด์ค์ด๋ฏ๋กDOMNode
์attribs
๊ฐ ์๊ธฐ ๋๋ฌธ์ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค. ์ฌ์ฐ.```
๊ทธ๋ ๋ค๋ฉด ์ ์ด๊ฒ์ด NextJS ์ฑ์์๋ง ๋ฐ์ํฉ๋๊น?
react-scripts typescript ์ฑ์ ์ค์ ํ๋๋ฐ ์๋ํฉ๋๋ค.
https://codesandbox.io/s/html-parsing-and-replacing-elements-yjtv7?file=/src/index.tsx
@Naxos84๋ ์ ๋ชจ๋ฅด๊ฒ ์ต๋๋ค. Next.js์๋ ๋ค๋ฅธ TypeScript ๊ตฌ์ฑ์ด ์์ต๋๋ค. ํด๊ฒฐ ๋ฐฉ๋ฒ์ @kakaeriel ์ ์๋ฃจ์ ์ ํ์ธํ์ธ์.
@kakaeriel ์ด PR์ ์ด์ด README.md
์ ํด๊ฒฐ ๋ฐฉ๋ฒ์ผ๋ก ์
๋ฐ์ดํธํ ์ ์๋์ง ์๋ ค์ฃผ์ธ์. ๋น์ ์ด ํ ์ ์๋ค๋ฉด, ๋๋ ํ ์ ์์ต๋๋ค. ๊ฐ์ฌ ํด์!
๋๋ ๊ทธ๊ฒ์ด NextJ์ ๊ด๋ จ์ด ์๋ค๊ณ ์๊ฐํ์ง ์์ต๋๋ค.
๋๋ ์กฐ๊ธ ๋ ์๋ํ๊ณ ์ด๋ป๊ฒ ๋ ํจ๊ณผ๊ฐ ์์๋ค.
@remarkablemark codesandbox๋ฅผ ๋ณ๊ฒฝํ๊ณ Stack Overflow์์ ์ธ๊ธํ console.log(domElement.constructor)
๋ฅผ ์ถ๊ฐํ์ต๋๋ค.
๊ฒฐ๊ณผ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
์ ๊ฐ์๊ธฐ ์๋ํ๋์ง ์ดํด๊ฐ๋์ง ์์ต๋๋ค!?
"๋ด"nextjs ํ๋ก์ ํธ์์ ์๋ํ์ง ์์ง๋ง.
์
๋ฐ์ดํธ: ๊ทธ๋ฆฌ๊ณ ์ด์ ๋ค์ ์๋ํ์ง ์์ต๋๋ค....
์ฌ์ฉ์ ์ ์ typeguard๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
const isElement = (domNode: DOMNode): domNode is Element => {
const isTag = domNode.type === "tag";
const hasAttributes = (domNode as Element).attribs !== undefined;
return isTag && hasAttributes;
};
const parserOptions: HTMLReactParserOptions = {
replace: domNode => {
if (isElement(domNode)) {
return <p>It's an element.</p>;
} else {
return <p>It's something else</p>;
}
},
};
์๋ ํ์ธ์ ์ฌ๋ฌ๋ถ, ์ ๋ ์ด ์๋ฃจ์ ์ผ๋ก Next.js 10.x์์
@natterstefan ์๋ฃจ์ ์ ๊ณต์ ํด ์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค!
NextJS์๋ ๊ฐ์ ๋ฌธ์ ๊ฐ ์์ต๋๋ค. ์์๋ ํญ์ ์ ์๋์ง ์์ต๋๋ค. domNode๋ฅผ any๋ก ์ ๋ ฅํ๊ณ ์ต์ ์ ํ์ HTMLReactParserOptions๋ก ๋ณํํ์ต๋๋ค. domhandler์ ์์๊ฐ SSR์์ ์๋ํ์ง ์์ต๋๊น?
const options = {
replace: (domNode: any) => {
// ...
}
} as HTMLReactParserOptions;
์๋ ์ฝ๋๋ก ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์์์ต๋๋ค. ๋์์ด ๋์๊ธฐ๋ฅผ ๋ฐ๋๋๋ค.
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์ด ์ง์นจ์ ๋ฐ๋ฅด๊ณ ๋์ผํ ๋ฌธ์ ๊ฐ ์์ต๋๋ค.
attribs
์์domNode
attribs
์์ฑ์ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค.typescript check
domNode
๋Element
์ ํ์ด ์๋๋ผDOMNode
์ธ์คํด์ค์ด๋ฏ๋กDOMNode
์attribs
์๊ธฐ ๋๋ฌธ์ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค. ์ฌ์ฐ.๊ทธ๋ฆฌ๊ณ ์ด ์กฐ๊ฑด์ผ๋ก
console.log(domNode instanceof Element)
ํ๋ฉด domNode์ ์ธ์คํด์ค๊ฐDOMNode
์ด๊ธฐ ๋๋ฌธ์ ํญ์ false๋ฅผ ๋ฐํํฉ๋๋ค.์ด์ ๋ํ ํด๊ฒฐ์ฑ ์ด ์์ต๋๋ค. ๋์์ด ๋ ์ ์์ต๋๋ค. domNode ๋งค๊ฐ๋ณ์์
Element
์ ํ์ ์ ์ํ์ต๋๋ค.