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