{"id":19521999,"url":"https://github.com/renuo/html2structured-block","last_synced_at":"2026-06-15T19:31:03.239Z","repository":{"id":146098026,"uuid":"616998637","full_name":"renuo/html2structured-block","owner":"renuo","description":"HTML to DatoCMS Structured Text Converter","archived":false,"fork":false,"pushed_at":"2023-03-22T11:53:26.000Z","size":374,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-05-27T08:01:49.504Z","etag":null,"topics":["converter","dato","datocms","html2dato","importer"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/renuo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-21T13:59:08.000Z","updated_at":"2023-03-27T06:55:06.000Z","dependencies_parsed_at":"2023-05-14T17:15:13.305Z","dependency_job_id":null,"html_url":"https://github.com/renuo/html2structured-block","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/renuo/html2structured-block","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/renuo%2Fhtml2structured-block","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/renuo%2Fhtml2structured-block/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/renuo%2Fhtml2structured-block/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/renuo%2Fhtml2structured-block/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/renuo","download_url":"https://codeload.github.com/renuo/html2structured-block/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/renuo%2Fhtml2structured-block/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34377872,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-15T02:00:07.085Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["converter","dato","datocms","html2dato","importer"],"created_at":"2024-11-11T00:36:14.904Z","updated_at":"2026-06-15T19:31:03.211Z","avatar_url":"https://github.com/renuo.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Simple HTML to DatoCMS Structured Text Converter\n**and some other utilities functions to speed up the migration from a Database to DatoCMS**\n\n[![Run Tests](https://github.com/renuo/html2structured-block/actions/workflows/main.yml/badge.svg)](https://github.com/renuo/html2structured-block/actions/workflows/main.yml)\n\nThis project provides a utility to convert HTML content into structured text format, which can be used with DatoCMS.\nThe converter handles various HTML elements and converts them into appropriate structured text blocks and inline elements.\n\n[Official Documentation](https://www.datocms.com/docs/content-management-api/resources/item/create) | \n[About Structured Blocks](https://www.datocms.com/docs/structured-text/migrating-content-to-structured-text)\n\n## How to Run\n\nInstall the required dependencies:\n```bash\nnpm install\n```\n\nInclude the module in your project:\n```javascript\nconst { HTML2DatoCMS } = require('./html2datocms.js');\n// client is a DatoCMS client, only needed for image upload (see below)\n// image_block_id is the ID of the image block in your DatoCMS project, only needed for image upload (see below)\nlet html2datocms = new HTML2DatoCMS(client, image_block_id);\n```\n\nUse the html2block function to convert HTML content to structured text:\n```javascript\nconst html = '\u003cp\u003eExample HTML content\u003c/p\u003e';\nconst structured_text = await html2datocms.html2block(html);\n```\n\n### Advanced Usage\n\nThe `html2datocms.js` module provides a number of functions that can be used to convert HTML content to structured text, upload images to DatoCMS, and fetch records from DatoCMS.\n\nThe following functions are available:\n\n**html2block**\n\nConverts HTML content to structured text.\n\n```javascript\nconst html = '\u003cp\u003eHello, world!\u003c/p\u003e';\nhtml2datocms.html2block(html)\n    .then(structured_text =\u003e {\n        console.log(structured_text);\n    })\n    .catch(error =\u003e {\n        console.error(error);\n    });\n```\n\n**boolToDatoCMS**\n\nConverts a boolean value (loaded from a JSON or CSV file) to a boolean value.\n\n```javascript\nconst boolValue = \"true\";\nconst datoCMSBoolValue = html2datocms.boolToDatoCMS(boolValue);\nconsole.log('DatoCMS boolean value:', datoCMSBoolValue);\n```\n\n**fetchRecords**\n\nFetches records from DatoCMS. Can be used to check if a record already exists before creating a new one or to fetch related records. See the [example.js](example/example.js) file for a complete example.\n\n```javascript\nconst itemType = 'YOUR_ITEM_TYPE';\nconst field = 'YOUR_FIELD';\nconst value = 'YOUR_VALUE';\n\nhtml2datocms.fetchRecords(itemType, field, value)\n    .then(records =\u003e {\n        console.log(records);\n    })\n    .catch(error =\u003e {\n        console.error(error);\n    });\n```\n\n**uploadToDatoCMS**\n\nUploads an image to DatoCMS. Also used by the `html2block` function to upload images referenced in `\u003cimg\u003e` tags. See the [example.js](example/example.js) file for a complete example.\n\n```javascript\nconst imageUrl = 'https://example.com/image.jpg';\n\nhtml2datocms.uploadToDatoCMS(imageUrl)\n    .then(upload =\u003e {\n        console.log(upload);\n    })\n    .catch(error =\u003e {\n        console.error(error);\n    });\n```\n\n### Complete Example\n\nCheck out the [example.js](example/example.js) file for a complete example of how to use the converter.\n\nThe model we used on DatoCMS is the following:\n\u003ccenter\u003e\n    \u003cimg src=\"example/my_item.png\" max-width=\"600px\"\u003e\n    \u003cimg src=\"example/image_block.png\" max-width=\"600px\"\u003e\n\u003c/center\u003e\n\n## How to Test\n\nRun the test suite:\n```bash\nnpm test\n```\n\nThis will run the tests defined in the `html2datocms.test.js` file.\n\n## DatoCMS\n\n**Setting up the Client**\n\nInstall the DatoCMS client package:\n```bash\nnpm install @datocms/cma-client-node\n```\n\nImport the `buildClient` function from the DatoCMS client package:\n```javascript\nconst buildClient = require('@datocms/cma-client-node').buildClient;\n```\n\nCreate the DatoCMS client using your API token:\n\n```javascript\nconst client = buildClient({ apiToken: 'YOUR_API_TOKEN' });\n```\n\nReplace `YOUR_API_TOKEN` with your actual DatoCMS API token.\n\nNow you can use the DatoCMS client with the functions provided in the `html2datocms.js` module, such as `uploadToDatoCMS` and `fetchRecords`.\n\n\n### Custom Tags\n\n**IMG-Tags/Image Block**\n\nFor `\u003cimg\u003e`-Tags, you need to add a custom Block to DatoCMS.\n\nYou will need to fetch the appropriate image block ID from your own DatoCMS account.\nTo obtain the correct image block ID, navigate to your DatoCMS project's settings.\n\nGo to:\n- Blocks Library\n- Create a Block called for example `Image Block`\n- Copy the `Model ID`\n\nAdd pass the Model ID of the `Image Block`\n```javascript\nlet html2datocms = new HTML2DatoCMS(client, image_block_id);\n```\n\n### Handling Unknown HTML Tags\n\nThis project is designed to handle a variety of common HTML tags and convert them into structured text.\nHowever, there may be cases where the code encounters an unknown or unsupported HTML tag.\nIn such scenarios, the code has a specific mechanism to manage these unknown tags.\n\nWhen the code identifies an unknown HTML tag, it generates an error block containing a message about the encountered issue.\nThis error block takes the form of a paragraph with a strong emphasis style.\nThe message will include `!!!!!!!! Error !!!!!!!!` followed by a brief description of the problem, such as `Unknown node: ` and the name of the unknown tag.\n\nBy generating an error block for each unknown tag, the code provides a clear indication of any issues encountered during the conversion process. This allows you to review the output in DatoCMS and identify any problematic tags that require manual intervention or adjustments to the code for proper handling.\n\nIf you need to extend the support for additional HTML tags, you can modify the code in the `buildTree` function accordingly, by adding new conditions or adjusting the existing ones to manage the new tags as needed.\n\nSee [html2datocms.js](html2datocms.js) for more details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frenuo%2Fhtml2structured-block","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frenuo%2Fhtml2structured-block","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frenuo%2Fhtml2structured-block/lists"}