1.0.2 • Published 12 months ago

html2sketch v1.0.2

Weekly downloads
98
License
MIT
Repository
github
Last release
12 months ago

English | 简体中文

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

1.0.2

12 months ago

1.0.1

1 year ago

1.0.0

1 year ago

1.0.0-beta.22

1 year ago

1.0.0-beta.20

1 year ago

1.0.0-beta.21

1 year ago

1.0.0-beta.19

1 year ago

1.0.0-beta.17

1 year ago

1.0.0-beta.18

1 year ago

1.0.0-beta.16

2 years ago

1.0.0-beta.15

2 years ago

1.0.0-beta.13

2 years ago

1.0.0-beta.14

2 years ago

1.0.0-beta.12

2 years ago

1.0.0-beta.11

2 years ago

1.0.0-beta.10

2 years ago

1.0.0-beta.7

3 years ago

1.0.0-beta.8

3 years ago

1.0.0-beta.9

3 years ago

1.0.0-beta.6

3 years ago

1.0.0-beta.3

3 years ago

1.0.0-beta.4

3 years ago

1.0.0-beta.5

3 years ago

1.0.0-beta.2

3 years ago

1.0.0-beta.1

3 years ago

0.4.7

3 years ago

0.4.6

3 years ago

0.4.5-0

4 years ago

0.4.5

4 years ago

0.4.4

4 years ago

0.4.3-1

4 years ago

0.4.3-0

4 years ago

0.4.3

4 years ago

0.4.2

4 years ago

0.4.2-0

4 years ago

0.4.1

4 years ago

0.4.1-5

4 years ago

0.4.1-4

4 years ago

0.4.1-3

4 years ago

0.4.1-2

4 years ago

0.4.1-1

4 years ago

0.4.1-0

4 years ago

0.4.0

4 years ago

0.4.0-1

4 years ago

0.4.0-0

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.2-1

4 years ago

0.2.2-0

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.16

4 years ago

0.1.14

4 years ago

0.1.15

4 years ago

0.1.10

4 years ago

0.1.11

4 years ago

0.1.12

4 years ago

0.1.13

4 years ago

0.1.9

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.5

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago