-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbookmark.cjs
8 lines (8 loc) · 2.12 KB
/
bookmark.cjs
1
2
3
4
5
6
7
8
/**
* @file bookmark.cjs
* @name netscape-bookmark-tree
* @version 2.1.1 Build.1684236336177
* @author ZMY
* @license MIT
*/
"use strict";var e=require("node:os"),t=require("parse5");function n(e,t,n){const r=" ";function i(e,a){return e.reduce((function(e,s){const{name:c,attributes:o,children:u}=t(s,a),l=a.concat(s),d=a.reduce((e=>e+r),n+r),p=function(e){return e?e.reduce((function(e,t){return`${e} ${t[0].toUpperCase()}="${t[1]}"`}),""):""}(o);let f;if(Array.isArray(u)){const e=i(u,l),t=[`<DT><H3${p}>${c}</H3>`,"<DL><p>",r+e,"</DL><p>"];0===u.length&&t.splice(2,1),f=t.join(d)}else f=`<DT><A${p}>${c}</A>`;return e?e+d+f:f}),"")}return e.map((function(e){const{children:n,name:a}=t(e);let s=i(n,[]);return s=`\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}${s}\n</DL><p>\n`,s=s.trimStart(),s}))}const r={parseHTML:e=>t.parseFragment(e).childNodes,getTag:e=>e.nodeName,getName:e=>e.childNodes[0].value,setAttrs(e,t){const{attrs:n}=t;return n.map((function(t){return e[t.name]=t.value,[t.name,t.value]}))}},i={parse:{each(e,t){const n=(i=t.parentPath)[i.length-1],r=t.index;var i;return e.index=r,n?(e.id=`${n.id}_${r}`,e.pid=n.id):e.id=r.toString(),e},setChildren(e,t){e.children=t}},stringify:{each:function(e){return e},eol:e.EOL}};exports.defaultOptions=i,exports.parse=function(e,t){return t=Object.assign({},i.parse,t),function(e,t,n,r){const i=t.replace(/<!DOCTYPE [\s\S]+?<\/TITLE>|<DT>|<p>/g,""),a=function t(i,a){const s=[];return i.forEach((function(i){const c=e.getTag(i);function o(t){const r=s.length;let o={name:e.getName(i)};const u=e.setAttrs(o,i);o=n(o,{parentPath:a,isLeaf:t,index:r,attributes:u,tag:c},i)||o,s.push(o)}if("a"===c)o(!0);else if("h3"===c||"h1"===c)o(!1);else if("dl"===c){const e=s[s.length-1],n=a.concat(e),c=t(i.childNodes,n);r(e,c)}})),s}(e.parseHTML(i),[]);return a}(r,e,t.each,t.setChildren)},exports.stringify=function(e,t){return n(e,(t=Object.assign({},i.stringify,t)).each,t.eol)};