-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbookmark.js
8 lines (8 loc) · 2.11 KB
/
bookmark.js
1
2
3
4
5
6
7
8
/**
* @file bookmark.js
* @name netscape-bookmark-tree
* @version 2.1.1 Build.1684236336177
* @author ZMY
* @license MIT
*/
import e from"node:os";import{parseFragment as n}from"parse5";function t(e,n,t){const r=" ";function i(e,a){return e.reduce((function(e,o){const{name:c,attributes:s,children:u}=n(o,a),l=a.concat(o),d=a.reduce((e=>e+r),t+r),f=function(e){return e?e.reduce((function(e,n){return`${e} ${n[0].toUpperCase()}="${n[1]}"`}),""):""}(s);let T;if(Array.isArray(u)){const e=i(u,l),n=[`<DT><H3${f}>${c}</H3>`,"<DL><p>",r+e,"</DL><p>"];0===u.length&&n.splice(2,1),T=n.join(d)}else T=`<DT><A${f}>${c}</A>`;return e?e+d+T:T}),"")}return e.map((function(e){const{children:t,name:a}=n(e);let o=i(t,[]);return o=`\n<!DOCTYPE NETSCAPE-Bookmark-file-1>\n\x3c!-- This is an automatically generated file.\n It will be read and overwritten.\n DO NOT EDIT! --\x3e\n<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">\n<TITLE>Bookmarks</TITLE>\n<H1>${a}</H1>\n<DL><p>\n${r}${o}\n</DL><p>\n`,o=o.trimStart(),o}))}const r={parseHTML:e=>n(e).childNodes,getTag:e=>e.nodeName,getName:e=>e.childNodes[0].value,setAttrs(e,n){const{attrs:t}=n;return t.map((function(n){return e[n.name]=n.value,[n.name,n.value]}))}},i={parse:{each(e,n){const t=(i=n.parentPath)[i.length-1],r=n.index;var i;return e.index=r,t?(e.id=`${t.id}_${r}`,e.pid=t.id):e.id=r.toString(),e},setChildren(e,n){e.children=n}},stringify:{each:function(e){return e},eol:e.EOL}};function a(e,n){return n=Object.assign({},i.parse,n),function(e,n,t,r){const i=n.replace(/<!DOCTYPE [\s\S]+?<\/TITLE>|<DT>|<p>/g,""),a=function n(i,a){const o=[];return i.forEach((function(i){const c=e.getTag(i);function s(n){const r=o.length;let s={name:e.getName(i)};const u=e.setAttrs(s,i);s=t(s,{parentPath:a,isLeaf:n,index:r,attributes:u,tag:c},i)||s,o.push(s)}if("a"===c)s(!0);else if("h3"===c||"h1"===c)s(!1);else if("dl"===c){const e=o[o.length-1],t=a.concat(e),c=n(i.childNodes,t);r(e,c)}})),o}(e.parseHTML(i),[]);return a}(r,e,n.each,n.setChildren)}function o(e,n){return t(e,(n=Object.assign({},i.stringify,n)).each,n.eol)}export{i as defaultOptions,a as parse,o as stringify};