UNPKG

7.89 kBMarkdownView Raw
1<img src="https://gw.alipayobjects.com/zos/antfincdn/R8sN%24GNdh6/language.svg" width="18"> English | [简体中文](./README.zh-CN.md)
2
3<p align="center">
4 <a href="https://github.com/ant-design/html2sketch">
5 <img src="https://gw.alipayobjects.com/zos/antfincdn/9qm%24x99yzk/Logo.png" height="200" width="200" alt="html2sketch"/>
6 </a>
7</p>
8
9<h1 align="center"><a href="https://ant-design.github.io/html2sketch/">html2sketch</a></h1>
10
11<div align="center">
12
13[![NPM version][npm-image]][npm-url] [![NPM version][npm-next-image]][npm-url] [![NPM downloads][download-image]][download-url] [![install size][npm-size]][npm-size-url]
14
15[![Test CI status][test-ci]][test-ci-url] ![Deploy CI][deploy-ci] [![Coverage][coverage]][codecov-url]
16
17[![ docs by dumi][dumi-url]](https://d.umijs.org/) [![Build With father][father-url]](https://github.com/umijs/father/)
18
19[![Gitpod ready-to-code][gitpod-badge]][gitpod-url]
20
21<!-- gitpod url -->
22
23[gitpod-badge]: https://img.shields.io/badge/Gitpod-ready--to--code-blue?logo=gitpod
24[gitpod-url]: https://gitpod.io/#https://github.com/ant-design/html2sketch
25
26<!-- umi url -->
27
28[dumi-url]: https://img.shields.io/badge/docs%20by-dumi-blue
29[father-url]: https://img.shields.io/badge/build%20with-father-028fe4.svg
30
31<!-- npm url -->
32
33[npm-image]: http://img.shields.io/npm/v/html2sketch.svg?style=flat-square&color=deepgreen&label=latest
34[npm-next-image]: https://img.shields.io/npm/v/html2sketch/next?label=next&style=flat-square
35[npm-url]: http://npmjs.org/package/html2sketch
36[npm-size]: https://img.shields.io/bundlephobia/minzip/html2sketch?color=deepgreen&label=gizpped%20size&style=flat-square
37[npm-size-url]: https://packagephobia.com/result?p=html2sketch
38
39<!-- coverage -->
40
41[coverage]: https://codecov.io/gh/ant-design/html2sketch/branch/master/graph/badge.svg
42[codecov-url]: https://codecov.io/gh/ant-design/html2sketch/branch/master
43
44<!-- Github CI -->
45
46[test-ci]: https://github.com/ant-design/html2sketch/workflows/Test%20CI/badge.svg
47[deploy-ci]: https://github.com/ant-design/html2sketch/workflows/Deploy%20CI/badge.svg
48[test-ci-url]: https://github.com/ant-design/html2sketch/actions?query=workflow%3ATest%20CI
49[deploy-ci-ci]: https://github.com/ant-design/html2sketch/actions?query=workflow%3ADeploy%20CI
50[download-image]: https://img.shields.io/npm/dm/html2sketch.svg?style=flat-square
51[download-url]: https://npmjs.org/package/html2sketch
52
53</div>
54
55## Introduction
56
57A module that transform HTML into Sketch JSON
58
59## Quick Guide
60
61### Install
62
63```bash
64npm i html2sketch --save
65```
66
67or
68
69```
70yarn add html2sketch
71```
72
73### Usage
74
75`html2sketch` includes three methods `nodeToLayer``nodeToGroup``nodeToSymbol`
76
77#### nodeToLayer
78
79this method transforms a DOM node into Sketch Objects without processing children of nodes
80
81```js
82import { nodeToLayer } from 'html2sketch';
83
84const fn = async () => {
85 // 1. get DOM node
86 const node = document.getElementById('id');
87
88 // 2. run nodeToLayer method
89 const layer = await nodeToLayer(node);
90
91 // 3. generate Sketch JSON
92 const sketchJSON = layer.toSketchJSON();
93
94 return sketchJSON;
95};
96
97fn().then((json) => {
98 console.log(json);
99});
100```
101
102#### nodeToGroup
103
104This method transforms a DOM node and its children into a Sketch Group Object
105
106```js
107import { nodeToGroup } from 'html2sketch';
108
109const fn = async () => {
110 // 1. get DOM node
111 const node = document.getElementById('id');
112
113 // 2. run nodeToGroup method
114 const group = await nodeToGroup(node);
115
116 // 3. generate Sketch JSON
117 const sketchJSON = group.toSketchJSON();
118
119 return sketchJSON;
120};
121
122fn().then((json) => {
123 console.log(json);
124});
125```
126
127#### nodeToSymbol
128
129This method transforms a DOM node and its children into a Sketch Symbol Object
130
131```js
132import { nodeToSymbol } from 'html2sketch';
133
134const fn = async () => {
135 // 1. get DOM node
136 const node = document.getElementById('id');
137
138 // 2. run nodeToSymbol method
139 const symbol = await nodeToSymbol(node);
140
141 // 3. generate Sketch JSON
142 const sketchJSON = symbol.toSketchJSON();
143
144 return sketchJSON;
145};
146
147fn().then((json) => {
148 console.log(json);
149});
150```
151
152## What's next step with Sketch JSON?
153
154the generated Sketch JSON strictly match with [Sketch File Format](https://developer.sketch.com/file-format/) Schema, So you can just synthesize the JSON according to the [Sketch File Format](https://developer.sketch.com/file-format/) into a '. Sketch 'file .
155
156So the Sketch file can be obtained simply by synthesizing the corresponding JSON into a '. Sketch 'file according to the Sketch file specification
157
158There is some community modules of synthesizing:
159
160- [sketch-json-api](https://github.com/ant-design/sketch-json-api)
161- [node-sketch](https://github.com/oscarotero/node-sketch)
162- [sketch-constructor](https://github.com/amzn/sketch-constructor)
163
164If you want to use the JSON object directly in sketch, you can use the [Sketch JSON](https://github.com/arvinxx/sketch-json) Plugin,which will allow you just paste JSON into Sketch.
165
166## Why?
167
168The main application of this module focus on C2D (Code to Design).This module mainly refers to [html-sketchapp](https://github.com/html-sketchapp/html-sketchapp).
169
170html-sketchapp can directly transforms any HTML page into a Sketch document without framework limit, but there are significant limitations:
171
172- **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;
173
174- **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](https://developer.sketch.com/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.
175
176- **html-sketchapp developed by JS ,lacking perfect type definition, the project architecture is not reasonable, the secondary development is difficult**
177
178### What html2sketch do?
179
180html2sketch makes a lot of optimization on the basis of html-sketchapp, and takes the essence to discard the dross:
181
182#### Enhancement of parsing ability
183
184html2sketch 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](https://github.com/ant-design/html2sketch/issues), I will solve it as soon as possible. The styles supported by this module are currently available on [解析用例](https://ant-design.github.io/html2sketch/e2e).
185
186#### Decouple from the Sketch App
187
188The 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.
189
190Thanks to the JSON generated by html2Sketch will strictly match [Sketch File Format](https://developer.sketch.com/file-format/) , you can directly synthesize it into a legal `.sketch` file by using [sketch-json-api](https://github.com/ant-design/sketch-json-api) or [node-sketch](https://github.com/oscarotero/node-sketch) . Therefore,based on html2sketch, it is possible to parse a web page and generate the Sketch file using any normal server.
191
192#### Development based on Typescript
193
194Developed 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.
195
196## Develop
197
198Refer to [Develop Guidelines](https://github.com/ant-design/html2sketch/guide)
199
200## License
201
202[MIT](./LICENSE)