Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

html2sketch

ant-design157kMIT1.0.2TypeScript support: included

parser HTML to Sketch

html, sketch, design, c2d

readme

English | 简体中文

html2sketch

html2sketch

NPM version NPM version NPM downloads install size

Test CI status Deploy CI Coverage

docs by dumi Build With father

Gitpod ready-to-code

Introduction

A module that transform HTML into Sketch JSON

Quick Guide

Install

npm i html2sketch --save

or

yarn add html2sketch

Usage

html2sketch includes three methods nodeToLayernodeToGroupnodeToSymbol

nodeToLayer

this method transforms a DOM node into Sketch Objects without processing children of nodes

import { nodeToLayer } from 'html2sketch';

const fn = async () => {
  // 1. get DOM node
  const node = document.getElementById('id');

  // 2. run nodeToLayer method
  const layer = await nodeToLayer(node);

  // 3. generate Sketch JSON
  const sketchJSON = layer.toSketchJSON();

  return sketchJSON;
};

fn().then((json) => {
  console.log(json);
});

nodeToGroup

This method transforms a DOM node and its children into a Sketch Group Object

import { nodeToGroup } from 'html2sketch';

const fn = async () => {
  // 1. get DOM node
  const node = document.getElementById('id');

  // 2. run nodeToGroup method
  const group = await nodeToGroup(node);

  // 3. generate Sketch JSON
  const sketchJSON = group.toSketchJSON();

  return sketchJSON;
};

fn().then((json) => {
  console.log(json);
});

nodeToSymbol

This method transforms a DOM node and its children into a Sketch Symbol Object

import { nodeToSymbol } from 'html2sketch';

const fn = async () => {
  // 1. get DOM node
  const node = document.getElementById('id');

  // 2. run nodeToSymbol method
  const symbol = await nodeToSymbol(node);

  // 3. generate Sketch JSON
  const sketchJSON = symbol.toSketchJSON();

  return sketchJSON;
};

fn().then((json) => {
  console.log(json);
});

What's next step with Sketch JSON?

the generated Sketch JSON strictly match with Sketch File Format Schema, So you can just synthesize the JSON according to the Sketch File Format into a '. Sketch 'file .

So the Sketch file can be obtained simply by synthesizing the corresponding JSON into a '. Sketch 'file according to the Sketch file specification

There is some community modules of synthesizing:

If you want to use the JSON object directly in sketch, you can use the Sketch JSON Plugin,which will allow you just paste JSON into Sketch.

Why?

The main application of this module focus on C2D (Code to Design).This module mainly refers to html-sketchapp.

html-sketchapp can directly transforms any HTML page into a Sketch document without framework limit, but there are significant limitations:

  • Not supported pseudoelements, overflow,some kind of gradient, transform, and other properties: As a result, many web pages transformed by html-sketchapp are not visually restored;

  • Although html-sketchapp don't dependent on the framework, it fully dependent on the Sketch App: Because JSON structure generated by html-sketchapp does not strictly match Sketch File Format ,it can't generate sketch document directly.What's more,it also lead to lacking of the ability to integrate on the server side.

  • html-sketchapp developed by JS ,lacking perfect type definition, the project architecture is not reasonable, the secondary development is difficult

What html2sketch do?

html2sketch makes a lot of optimization on the basis of html-sketchapp, and takes the essence to discard the dross:

Enhancement of parsing ability

html2sketch supports most web page styles which is not supported by html-sketchapp, such as pseudo-elements,radial gradient, text overflows, and so on.This is important for transforming result.Perhaps reproducing 80% using html-sketchapp, but 95% or more using html2sketch. Of course, the parsing capability of the module also needs to be improved step by step by covering the parsing scene. If you encounter any incorrect parsing situation under the real word, please be sure to submit an issue, I will solve it as soon as possible. The styles supported by this module are currently available on 解析用例.

Decouple from the Sketch App

The biggest difference at the functional level of html2sketch is its decoupling from the Sketch App.The relevant parsing capabilities (Svg, image, font, and so on) are all built from scratch.

Thanks to the JSON generated by html2Sketch will strictly match Sketch File Format , you can directly synthesize it into a legal .sketch file by using sketch-json-api or node-sketch . Therefore,based on html2sketch, it is possible to parse a web page and generate the Sketch file using any normal server.

Development based on Typescript

Developed with Typescript makes it a good experience for both consumers and producers.Whether it's in the organization of the project, code comments, or unit testing, there are guarantees.

Develop

Refer to Develop Guidelines

License

MIT

changelog

html2sketch 更新日志

1.0.2 (2023-05-22)

🐛 Bug Fixes

  • 调整 engine 到 node 14 (22bbb44)

1.0.1 (2023-02-16)

🐛 Bug Fixes

  • cssRules error should not block parse (2257640)

1.0.0 (2023-02-09)

♻ Code Refactoring

  • rename the symbol function (09d0de9)

✨ Features

  • input support overflow (0ff7b55), closes #177
  • support almost full css transform (f7bb06d), closes #148
  • support overflow element (f4b017b), closes #178
  • support rad angle line-gradient (9860090), closes #96
  • support rotate & clip-path (35f6628), closes #180
  • 优化抓取 base64 图片方法 (07dd128)
  • 提供自定义 fetch 方法的能力 (28ed276)
  • 新增固定宽高的能力 (f2df2e5)

🐛 Bug Fixes

  • adjust text and svg resizing constraint (9fc8970)
  • fix absolute position margin parsing (120ba39)
  • fix overflow hidden node use clipping mask (defb941)
  • fix overflow hidden node use clipping mask (055f25d)
  • fix password input text (c8ff928)
  • fix placeholder parsing without css rules (505a390)
  • fix position bug due to sketch text align parsing error (1dce98f), closes #51
  • fix pseudo elt style issue and add test (5c727a3)
  • fix resizingConstraint func and update test (c39f843)
  • gradient parse in safari (aa374e5), closes #185
  • parse linear-gradient (90f3ed8), closes #184
  • set default resizingConstraint to left and top (f897275)
  • should parse textarea value and placeholder (06d70ac), closes #161
  • single child overflow should not be mask (1acab12), closes #181
  • try to fix babel problem (398f825)
  • 修正 textStyle 缺失导致修改文本造成报错的问题 (1a44244)

💥 BREAKING CHANGES

  • rename nodeToSketchSymbol to nodeToSymbol

1.0.0-beta.22 (2023-02-09)

🐛 Bug Fixes

1.0.0-beta.21 (2023-02-09)

🐛 Bug Fixes

1.0.0-beta.20 (2023-02-06)

🐛 Bug Fixes

  • single child overflow should not be mask (1acab12), closes #181

1.0.0-beta.19 (2023-02-02)

✨ Features

1.0.0-beta.18 (2023-01-16)

✨ Features

1.0.0-beta.17 (2023-01-13)

✨ Features

1.0.0-beta.16 (2022-10-20)

🐛 Bug Fixes

  • 修正 textStyle 缺失导致修改文本造成报错的问题 (1a44244)

1.0.0-beta.15 (2022-09-14)

🐛 Bug Fixes

  • should parse textarea value and placeholder (06d70ac), closes #161

1.0.0-beta.14 (2022-08-31)

1.0.0-beta.13 (2022-08-31)

1.0.0-beta.12 (2022-07-19)

🐛 Bug Fixes

  • try to fix babel problem (398f825)

1.0.0-beta.11 (2022-04-09)

1.0.0-beta.10 (2022-04-09)

✨ Features

  • support almost full css transform (f7bb06d), closes #148
  • 新增固定宽高的能力 (f2df2e5)

🐛 Bug Fixes

  • fix absolute position margin parsing (120ba39)
  • fix resizingConstraint func and update test (c39f843)

1.0.0-beta.9 (2021-09-13)

🐛 Bug Fixes

  • adjust text and svg resizing constraint (9fc8970)

1.0.0-beta.8 (2021-09-13)

✨ Features

  • support rad angle line-gradient (9860090), closes #96

1.0.0-beta.7 (2021-09-13)

🐛 Bug Fixes

  • fix pseudo elt style issue and add test (5c727a3)
  • set default resizingConstraint to left and top (f897275)

1.0.0-beta.6 (2021-06-29)

🐛 Bug Fixes

  • fix overflow hidden node use clipping mask (defb941)
  • fix overflow hidden node use clipping mask (055f25d)

1.0.0-beta.5 (2021-06-23)

🐛 Bug Fixes

  • fix password input text (c8ff928)

1.0.0-beta.4 (2021-06-23)

✨ Features

  • 优化抓取 base64 图片方法 (07dd128)

1.0.0-beta.3 (2021-06-22)

✨ Features

  • 提供自定义 fetch 方法的能力 (28ed276)

🐛 Bug Fixes

  • fix placeholder parsing without css rules (505a390)

1.0.0-beta.2 (2021-05-13)

🐛 Bug Fixes

  • fix position bug due to sketch text align parsing error (1dce98f), closes #51

1.0.0-beta.1 (2021-05-08)

♻ Code Refactoring

  • rename the symbol function (09d0de9)

💥 BREAKING CHANGES

  • rename nodeToSketchSymbol to nodeToSymbol

0.4.7 (2021-05-08)

🐛 Bug Fixes

0.4.6 (2020-11-04)

🐛 Bug Fixes

  • :bug: fix: fix a color string parser problem (9726096)

0.4.5 (2020-10-21)

🐛 Bug Fixes

0.4.4 (2020-10-21)

🐛 Bug Fixes

0.4.3 (2020-10-19)

✨ New Features

  • ✨ feat: support svg radial gradient (90f9847)
  • ✨ feat: support svg transform (d0c0075)

🐛 Bug Fixes

0.4.2 (2020-09-26)

🐛 Bug Fixes

0.4.1 (2020-09-24)

✨ New Features

🐛 Bug Fixes

  • Fix background svg & png parsing (8e38563)
  • Fix background png position (9a42cf3)
  • Fix background svg parsing (51151c7)
  • Fix symbol override (#33) (aa0bb2d), closes #33
  • Fix pseudo absolute XY (4140e2f)

0.4.0 (2020-09-12)

✨ New Features

  • ✨ feat: Parse svg illustration(95%) (3c2e1ef)

🐛 Bug Fixes

0.3.1 (2020-09-11)

🐛 Bug Fixes

0.3.0 (2020-09-07)

✨ New Features

🐛 Bug Fixes

0.2.2-1 (2020-08-31)

✨ New Features

🐛 Bug Fixes

0.2.2-0 (2020-08-27)

🐛 Bug Fixes

  • fix: 优化生成 symbol 名称 (289ffb8)

0.2.1 (2020-08-27)

🐛 Bug Fixes

  • fix: 修正配置项识别 (fb466ec)

0.2.0 (2020-08-26)

  • 💥 更新到 dumi 架构 (1a96c4f)

✨ New Features

  • feat: 初步集成 layout 能力 (194ed74)
  • feat: 完成 Modal 的 symbol 定义 (bd09b32)
  • feat: 完成 Modal 的 symbol 定义 (a0f3682)
  • feat: 添加 modal layout 识别 (8a68e27)
  • feat: 添加 symbol 名称的支持 (5a901e6)
  • feat: 添加默认的支持的 symbol layout (907556d)

🐛 Bug Fixes

  • fix: 修复部分样式解析问题 (ddfceb2)

0.1.15 (2020-08-10)

🐛 Bug Fixes

  • fix: 修复转换表格时无法转换边框的样式 (557191c)

0.1.14 (2020-08-10)

🐛 Bug Fixes

  • fix: 修复空 svg 导致的报错 (6e379a7)

0.1.13 (2020-08-08)

🐛 Bug Fixes

  • fix: 清理空图层的解析 (5bf109a)
  • fix: 清理空图层的解析 (72443b0)

0.1.12 (2020-08-08)

✨ New Features

🐛 Bug Fixes

  • fix: 修复伪类的解析样式 (3d2d99f)
  • fix: 修复文本的高度解析 (e1e128c)
  • fix: 修复文本的高度解析 (ada6502)
  • fix: 修复文本解析 (02d0847)
  • fix: 修复部分样式解析错误 (eefccdb)

  • feat: 添加伪类文本的解析 (e3b56fb)

0.1.1 (2020-07-27)

🐛 Bug Fixes

0.1.0 (2020-07-26)

✨ New Features

  • feat: 完成 SVG 的图形解析 (42b2f34)
  • feat: 完成形状解析转换 (2961eb4)
  • feat: 完成形状解析转换 (a6c08a7)
  • feat: 添加 ShapePath 类 (ced0b8d)
  • feat: 添加 Svg 支持 (9825978)

🐛 Bug Fixes

  • fix: 修复 Svg 解析后尺寸问题 (338857c)
  • fix: 修复大部分单路径识别的问题 (2f16b18)
  • fix: 修复循环问题 (3518125)
  • fix: 修复部分路径节点丢失的问题 (efe24ae)

0.0.3 (2020-07-20)

✨ New Features

  • feat: 初步完成 symbol 构建流程 (1da1d31)
  • feat: 添加渐变模型 (6ca272a)
  • feat: 添加解析伪类能力 (f20e6f2)

🐛 Bug Fixes

  • fix: 修复部分转换问题 (f2b1728)