Answer the question
In order to leave comments, you need to log in
How to convert HTML string to array?
We have a wiki in English on the project. language, in which we want to automatically add Russian translations via the Deepl API.
And the database text is stored as HTML.
The task seemed simple, just pass this string to Deepl and store the translation in the database. The translator himself sees the html markup well and returns a good translation in 90% of cases. However, if there is code in html (code tag), then, for example, MySQL code, the translator translates it too. We decided to split the html into tags and convert it to an array.
const jsdom = require('jsdom')
const dom = new JSDOM(html)
let arr = [...dom.window.document.body.children].map((child) => child.outerHTML)
[
"<p>Пример</p>",
"<co d e class="hljs language-css">.test {text-decoration: none;}</co d e>",
"<co d e class="hljs javascript">console.log('Test');</co d e>",
"<p>Пример</p>"
]
<p>This is my docker-compose.yml file</p>
<pre><code>version: "3.7"
services:
db:
build: database
container_name: db
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
volumes:
- ./pgdata:/var/lib/postgressql/data
- ./database/migrations:/migrations
ports:
- "5432:5432"
</code></pre>
<p>My end objective is that it should first run the parent's entry point command and then run mine.</p>
const arr = [
"<p>This is my docker-compose.yml file</p>",
"<pre><code>version: "3.7"
services:
db:
build: database
container_name: db
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
volumes:
- ./pgdata:/var/lib/postgressql/data
- ./database/migrations:/migrations
ports:
- "5432:5432"
</code></pre>",
"<p>My end objective is that it should first run the parent's entry point command and then run mine.</p>"
];
Answer the question
In order to leave comments, you need to log in
I recommend reading something about working with tree data structures or about parsing a JSON string, because your task is just about that. I can advise you to use the htmlparser2 library , where you can control parsing by the necessary tags or their attributes. As an experiment, you can try to cut , but the main thing is not to forget the position, so that you can put it back later <code>*</code>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question