Himalaya biến HTML thành JSON, thế là xong. Himalaya là đồng bộ và không yêu cầu bất kỳ cuộc gọi lại phức tạp nào
xử lý kỳ lạ
Himalaya xử lý rất nhiều trường hợp bên lề của HTML, như
- Đóng các thẻ chưa đóng
...
- Bỏ qua các thẻ đóng phụ
import fs from 'fs' import {parse} from 'himalaya' const html = fs.readFileSync['/webpage.html', {encoding: 'utf8'}] const json = parse[html] console.log['👉', json]
0 - Xử lý đúng cách các thẻ void như
import fs from 'fs' import {parse} from 'himalaya' const html = fs.readFileSync['/webpage.html', {encoding: 'utf8'}] const json = parse[html] console.log['👉', json]
1 vàimport fs from 'fs' import {parse} from 'himalaya' const html = fs.readFileSync['/webpage.html', {encoding: 'utf8'}] const json = parse[html] console.log['👉', json]
2 - Xử lý đúng các thẻ tự đóng như
import fs from 'fs' import {parse} from 'himalaya' const html = fs.readFileSync['/webpage.html', {encoding: 'utf8'}] const json = parse[html] console.log['👉', json]
3 - Xử lý
import fs from 'fs' import {parse} from 'himalaya' const html = fs.readFileSync['/webpage.html', {encoding: 'utf8'}] const json = parse[html] console.log['👉', json]
4 vàimport fs from 'fs' import {parse} from 'himalaya' const html = fs.readFileSync['/webpage.html', {encoding: 'utf8'}] const json = parse[html] console.log['👉', json]
5 - Không phân tích cú pháp nội dung của các thẻ
,
import fs from 'fs' import {parse} from 'himalaya' const html = fs.readFileSync['/webpage.html', {encoding: 'utf8'}] const json = parse[html] console.log['👉', json]
7 và HTML5import fs from 'fs' import {parse} from 'himalaya' const html = fs.readFileSync['/webpage.html', {encoding: 'utf8'}] const json = parse[html] console.log['👉', json]
8
Giữ nguyên khoảng trắng
Himalaya không cắt góc và trả về một đại diện chính xác của HTML được cung cấp. Để xóa khoảng trắng, hãy xử lý hậu kỳ JSON;
Vị trí dòng, cột và chỉ mục
Himalaya có thể bao gồm vị trí bắt đầu và kết thúc của các nút trong đầu ra phân tích cú pháp. Để kích hoạt tính năng này, bạn có thể vượt qua
import fs from 'fs' import {parse} from 'himalaya' const html = fs.readFileSync['/webpage.html', {encoding: 'utf8'}] const json = parse[html] console.log['👉', json]9 phần mở rộng của
const html = '0 vớiHello world' const json = window.himalaya.parse[html] console.log['👉', json]
const html = '1Hello world' const json = window.himalaya.parse[html] console.log['👉', json]
import { parse, parseDefaults } from 'himalaya' parse['', { ...parseDefaults, includePositions: true }] /* => [ { "type": "element", "tagName": "img", "attributes": [], "children": [], "position": { "start": { "index": 0, "line": 0, "column": 0 }, "end": { "index": 5, "line": 0, "column": 5 } } } ] */
Quay lại HTML
Himalaya cung cấp một phương pháp
const html = '2. Ví dụ sau phân tích cú pháp HTML thành JSON sau đó phân tích lại JSON thành HTMLHello world' const json = window.himalaya.parse[html] console.log['👉', json]
import fs from 'fs' import {parse, stringify} from 'himalaya' const html = fs.readFileSync['/webpage.html', {encoding: 'utf8'}] const json = parse[html] fs.writeFileSync['/webpage.html', stringify[json]]
Tại sao là "Himalaya"?
Đầu tiên, bạn bè của tôi không hữu ích. Ngoại trừ Josh, Josh đã hỗ trợ tôi
Trong khi tôi đang kiểm tra trình phân tích cú pháp, tôi đã tải xuống trang chủ Twitter của mình và nhận được một đốm màu JSON khổng lồ. Trình chỉnh sửa mã Sublime Text của tôi có một bản đồ thu nhỏ và nhìn nghiêng dữ liệu trông giống như một dãy núi không bao giờ kết thúc. Ngoài ra, "himalaya" có H, M, L trong đó