view-sidebar.js 96 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247
  1. var sidebar = sidebar || {};
  2. var base = base || require('./base');
  3. sidebar.Sidebar = class {
  4. constructor(host, id) {
  5. this._host = host;
  6. this._id = id ? ('-' + id) : '';
  7. this._stack = [];
  8. this._closeSidebarHandler = () => {
  9. this._pop();
  10. };
  11. this._closeSidebarKeyDownHandler = (e) => {
  12. if (e.keyCode == 27) {
  13. e.preventDefault();
  14. this._pop();
  15. }
  16. };
  17. }
  18. _getElementById(id) {
  19. return this._host.document.getElementById(id + this._id);
  20. }
  21. open(content, title) {
  22. this.close();
  23. this.push(content, title);
  24. }
  25. close() {
  26. this._deactivate();
  27. this._stack = [];
  28. this._hide();
  29. }
  30. push(content, title) {
  31. const item = { title: title, content: content };
  32. this._stack.push(item);
  33. this._activate(item);
  34. }
  35. _pop() {
  36. this._deactivate();
  37. if (this._stack.length > 0) {
  38. this._stack.pop();
  39. }
  40. if (this._stack.length > 0) {
  41. this._activate(this._stack[this._stack.length - 1]);
  42. }
  43. else {
  44. this._hide();
  45. }
  46. }
  47. _hide() {
  48. const sidebar = this._getElementById('sidebar');
  49. if (sidebar) {
  50. sidebar.style.width = '0px';
  51. }
  52. const container = this._getElementById('graph');
  53. if (container) {
  54. container.style.width = '100%';
  55. container.focus();
  56. }
  57. }
  58. _deactivate() {
  59. const sidebar = this._getElementById('sidebar');
  60. if (sidebar) {
  61. const closeButton = this._getElementById('sidebar-closebutton');
  62. if (closeButton) {
  63. closeButton.removeEventListener('click', this._closeSidebarHandler);
  64. closeButton.style.color = '#f8f8f8';
  65. }
  66. this._host.document.removeEventListener('keydown', this._closeSidebarKeyDownHandler);
  67. }
  68. }
  69. _activate(item) {
  70. const sidebar = this._getElementById('sidebar');
  71. if (sidebar) {
  72. sidebar.innerHTML = '';
  73. const title = this._host.document.createElement('h1');
  74. title.classList.add('sidebar-title');
  75. title.innerHTML = item.title ? item.title.toUpperCase() : '';
  76. sidebar.appendChild(title);
  77. const closeButton = this._host.document.createElement('a');
  78. closeButton.classList.add('sidebar-closebutton');
  79. closeButton.setAttribute('id', 'sidebar-closebutton');
  80. closeButton.setAttribute('href', 'javascript:void(0)');
  81. closeButton.innerHTML = '×';
  82. closeButton.addEventListener('click', this._closeSidebarHandler);
  83. sidebar.appendChild(closeButton);
  84. const content = this._host.document.createElement('div');
  85. content.classList.add('sidebar-content');
  86. content.setAttribute('id', 'sidebar-content');
  87. sidebar.appendChild(content);
  88. if (typeof item.content == 'string') {
  89. content.innerHTML = item.content;
  90. }
  91. else if (item.content instanceof Array) {
  92. for (const element of item.content) {
  93. content.appendChild(element);
  94. }
  95. }
  96. else {
  97. content.appendChild(item.content);
  98. }
  99. sidebar.style.width = 'min(calc(100% * 0.6), 500px)';
  100. this._host.document.addEventListener('keydown', this._closeSidebarKeyDownHandler);
  101. }
  102. const container = this._getElementById('graph');
  103. if (container) {
  104. container.style.width = 'max(40vw, calc(100vw - 500px))';
  105. }
  106. }
  107. };
  108. sidebar.NodeSidebar = class {
  109. constructor(host, node) {
  110. this._host = host;
  111. this._node = node;
  112. this._elements = [];
  113. this._attributes = [];
  114. this._inputs = [];
  115. this._outputs = [];
  116. if (node.type) {
  117. let showDocumentation = null;
  118. const type = node.type;
  119. if (type && (type.description || type.inputs || type.outputs || type.attributes)) {
  120. showDocumentation = {};
  121. showDocumentation.text = type.nodes ? '\u0192': '?';
  122. showDocumentation.callback = () => {
  123. this._raise('show-documentation', null);
  124. };
  125. }
  126. this._addProperty('type', new sidebar.ValueTextView(this._host, node.type.name, showDocumentation));
  127. if (node.type.module) {
  128. this._addProperty('module', new sidebar.ValueTextView(this._host, node.type.module));
  129. }
  130. }
  131. if (node.name) {
  132. this._addProperty('name', new sidebar.ValueTextView(this._host, node.name));
  133. }
  134. if (node.location) {
  135. this._addProperty('location', new sidebar.ValueTextView(this._host, node.location));
  136. }
  137. if (node.description) {
  138. this._addProperty('description', new sidebar.ValueTextView(this._host, node.description));
  139. }
  140. if (node.device) {
  141. this._addProperty('device', new sidebar.ValueTextView(this._host, node.device));
  142. }
  143. const attributes = node.attributes;
  144. if (attributes && attributes.length > 0) {
  145. const sortedAttributes = node.attributes.slice();
  146. sortedAttributes.sort((a, b) => {
  147. const au = a.name.toUpperCase();
  148. const bu = b.name.toUpperCase();
  149. return (au < bu) ? -1 : (au > bu) ? 1 : 0;
  150. });
  151. this._addHeader('Attributes');
  152. for (const attribute of sortedAttributes) {
  153. this._addAttribute(attribute.name, attribute);
  154. }
  155. }
  156. const inputs = node.inputs;
  157. if (inputs && inputs.length > 0) {
  158. this._addHeader('Inputs');
  159. for (const input of inputs) {
  160. this._addInput(input.name, input);
  161. }
  162. }
  163. const outputs = node.outputs;
  164. if (outputs && outputs.length > 0) {
  165. this._addHeader('Outputs');
  166. for (const output of outputs) {
  167. this._addOutput(output.name, output);
  168. }
  169. }
  170. const separator = this._host.document.createElement('div');
  171. separator.className = 'sidebar-view-separator';
  172. this._elements.push(separator);
  173. }
  174. render() {
  175. return this._elements;
  176. }
  177. _addHeader(title) {
  178. const headerElement = this._host.document.createElement('div');
  179. headerElement.className = 'sidebar-view-header';
  180. headerElement.innerText = title;
  181. this._elements.push(headerElement);
  182. }
  183. _addProperty(name, value) {
  184. const item = new sidebar.NameValueView(this._host, name, value);
  185. this._elements.push(item.render());
  186. }
  187. _addAttribute(name, attribute) {
  188. const item = new NodeAttributeView(this._host, attribute);
  189. item.on('show-graph', (sender, graph) => {
  190. this._raise('show-graph', graph);
  191. });
  192. const view = new sidebar.NameValueView(this._host, name, item);
  193. this._attributes.push(view);
  194. this._elements.push(view.render());
  195. }
  196. _addInput(name, input) {
  197. if (input.arguments.length > 0) {
  198. const view = new sidebar.ParameterView(this._host, input);
  199. view.on('export-tensor', (sender, tensor) => {
  200. this._raise('export-tensor', tensor);
  201. });
  202. view.on('error', (sender, tensor) => {
  203. this._raise('error', tensor);
  204. });
  205. const item = new sidebar.NameValueView(this._host, name, view);
  206. this._inputs.push(item);
  207. this._elements.push(item.render());
  208. }
  209. }
  210. _addOutput(name, output) {
  211. if (output.arguments.length > 0) {
  212. const item = new sidebar.NameValueView(this._host, name, new sidebar.ParameterView(this._host, output));
  213. this._outputs.push(item);
  214. this._elements.push(item.render());
  215. }
  216. }
  217. toggleInput(name) {
  218. for (const input of this._inputs) {
  219. if (name == input.name) {
  220. input.toggle();
  221. }
  222. }
  223. }
  224. on(event, callback) {
  225. this._events = this._events || {};
  226. this._events[event] = this._events[event] || [];
  227. this._events[event].push(callback);
  228. }
  229. _raise(event, data) {
  230. if (this._events && this._events[event]) {
  231. for (const callback of this._events[event]) {
  232. callback(this, data);
  233. }
  234. }
  235. }
  236. };
  237. sidebar.NameValueView = class {
  238. constructor(host, name, value) {
  239. this._host = host;
  240. this._name = name;
  241. this._value = value;
  242. const nameElement = this._host.document.createElement('div');
  243. nameElement.className = 'sidebar-view-item-name';
  244. const nameInputElement = this._host.document.createElement('input');
  245. nameInputElement.setAttribute('type', 'text');
  246. nameInputElement.setAttribute('value', name);
  247. nameInputElement.setAttribute('title', name);
  248. nameInputElement.setAttribute('readonly', 'true');
  249. nameElement.appendChild(nameInputElement);
  250. const valueElement = this._host.document.createElement('div');
  251. valueElement.className = 'sidebar-view-item-value-list';
  252. for (const element of value.render()) {
  253. valueElement.appendChild(element);
  254. }
  255. this._element = this._host.document.createElement('div');
  256. this._element.className = 'sidebar-view-item';
  257. this._element.appendChild(nameElement);
  258. this._element.appendChild(valueElement);
  259. }
  260. get name() {
  261. return this._name;
  262. }
  263. render() {
  264. return this._element;
  265. }
  266. toggle() {
  267. this._value.toggle();
  268. }
  269. };
  270. sidebar.SelectView = class {
  271. constructor(host, values, selected) {
  272. this._host = host;
  273. this._elements = [];
  274. this._values = values;
  275. const selectElement = this._host.document.createElement('select');
  276. selectElement.setAttribute('class', 'sidebar-view-item-select');
  277. selectElement.addEventListener('change', (e) => {
  278. this._raise('change', this._values[e.target.selectedIndex]);
  279. });
  280. this._elements.push(selectElement);
  281. for (const value of values) {
  282. const optionElement = this._host.document.createElement('option');
  283. optionElement.innerText = value.name || '';
  284. if (value == selected) {
  285. optionElement.setAttribute('selected', 'selected');
  286. }
  287. selectElement.appendChild(optionElement);
  288. }
  289. }
  290. render() {
  291. return this._elements;
  292. }
  293. on(event, callback) {
  294. this._events = this._events || {};
  295. this._events[event] = this._events[event] || [];
  296. this._events[event].push(callback);
  297. }
  298. _raise(event, data) {
  299. if (this._events && this._events[event]) {
  300. for (const callback of this._events[event]) {
  301. callback(this, data);
  302. }
  303. }
  304. }
  305. };
  306. sidebar.ValueTextView = class {
  307. constructor(host, value, action) {
  308. this._host = host;
  309. this._elements = [];
  310. const element = this._host.document.createElement('div');
  311. element.className = 'sidebar-view-item-value';
  312. this._elements.push(element);
  313. if (action) {
  314. this._action = this._host.document.createElement('div');
  315. this._action.className = 'sidebar-view-item-value-expander';
  316. this._action.innerHTML = action.text;
  317. this._action.addEventListener('click', () => {
  318. action.callback();
  319. });
  320. element.appendChild(this._action);
  321. }
  322. const list = Array.isArray(value) ? value : [ value ];
  323. let className = 'sidebar-view-item-value-line';
  324. for (const item of list) {
  325. const line = this._host.document.createElement('div');
  326. line.className = className;
  327. line.innerText = item;
  328. element.appendChild(line);
  329. className = 'sidebar-view-item-value-line-border';
  330. }
  331. }
  332. render() {
  333. return this._elements;
  334. }
  335. toggle() {
  336. }
  337. };
  338. class NodeAttributeView {
  339. constructor(host, attribute) {
  340. this._host = host;
  341. this._attribute = attribute;
  342. this._element = this._host.document.createElement('div');
  343. this._element.className = 'sidebar-view-item-value';
  344. const type = this._attribute.type;
  345. if (type) {
  346. this._expander = this._host.document.createElement('div');
  347. this._expander.className = 'sidebar-view-item-value-expander';
  348. this._expander.innerText = '+';
  349. this._expander.addEventListener('click', () => {
  350. this.toggle();
  351. });
  352. this._element.appendChild(this._expander);
  353. }
  354. const value = this._attribute.value;
  355. switch (type) {
  356. case 'graph': {
  357. const line = this._host.document.createElement('div');
  358. line.className = 'sidebar-view-item-value-line-link';
  359. line.innerHTML = value.name;
  360. line.addEventListener('click', () => {
  361. this._raise('show-graph', value);
  362. });
  363. this._element.appendChild(line);
  364. break;
  365. }
  366. case 'function': {
  367. const line = this._host.document.createElement('div');
  368. line.className = 'sidebar-view-item-value-line-link';
  369. line.innerHTML = type === value.type.name;
  370. line.addEventListener('click', () => {
  371. this._raise('show-graph', value.type);
  372. });
  373. this._element.appendChild(line);
  374. break;
  375. }
  376. default: {
  377. let content = new sidebar.Formatter(value, type).toString();
  378. if (content && content.length > 1000) {
  379. content = content.substring(0, 1000) + '\u2026';
  380. }
  381. if (content && typeof content === 'string') {
  382. content = content.split('<').join('&lt;').split('>').join('&gt;');
  383. }
  384. const line = this._host.document.createElement('div');
  385. line.className = 'sidebar-view-item-value-line';
  386. line.innerHTML = content ? content : '&nbsp;';
  387. this._element.appendChild(line);
  388. }
  389. }
  390. }
  391. render() {
  392. return [ this._element ];
  393. }
  394. toggle() {
  395. if (this._expander.innerText == '+') {
  396. this._expander.innerText = '-';
  397. const typeLine = this._host.document.createElement('div');
  398. typeLine.className = 'sidebar-view-item-value-line-border';
  399. const type = this._attribute.type;
  400. const value = this._attribute.value;
  401. if (type == 'tensor' && value && value.type) {
  402. typeLine.innerHTML = 'type: ' + '<code><b>' + value.type.toString() + '</b></code>';
  403. this._element.appendChild(typeLine);
  404. }
  405. else {
  406. typeLine.innerHTML = 'type: ' + '<code><b>' + this._attribute.type + '</b></code>';
  407. this._element.appendChild(typeLine);
  408. }
  409. const description = this._attribute.description;
  410. if (description) {
  411. const descriptionLine = this._host.document.createElement('div');
  412. descriptionLine.className = 'sidebar-view-item-value-line-border';
  413. descriptionLine.innerHTML = description;
  414. this._element.appendChild(descriptionLine);
  415. }
  416. if (this._attribute.type == 'tensor' && value) {
  417. const state = value.state;
  418. const valueLine = this._host.document.createElement('div');
  419. valueLine.className = 'sidebar-view-item-value-line-border';
  420. const contentLine = this._host.document.createElement('pre');
  421. contentLine.innerHTML = state || value.toString();
  422. valueLine.appendChild(contentLine);
  423. this._element.appendChild(valueLine);
  424. }
  425. }
  426. else {
  427. this._expander.innerText = '+';
  428. while (this._element.childElementCount > 2) {
  429. this._element.removeChild(this._element.lastChild);
  430. }
  431. }
  432. }
  433. on(event, callback) {
  434. this._events = this._events || {};
  435. this._events[event] = this._events[event] || [];
  436. this._events[event].push(callback);
  437. }
  438. _raise(event, data) {
  439. if (this._events && this._events[event]) {
  440. for (const callback of this._events[event]) {
  441. callback(this, data);
  442. }
  443. }
  444. }
  445. }
  446. sidebar.ParameterView = class {
  447. constructor(host, list) {
  448. this._list = list;
  449. this._elements = [];
  450. this._items = [];
  451. for (const argument of list.arguments) {
  452. const item = new sidebar.ArgumentView(host, argument);
  453. item.on('export-tensor', (sender, tensor) => {
  454. this._raise('export-tensor', tensor);
  455. });
  456. item.on('error', (sender, tensor) => {
  457. this._raise('error', tensor);
  458. });
  459. this._items.push(item);
  460. this._elements.push(item.render());
  461. }
  462. }
  463. render() {
  464. return this._elements;
  465. }
  466. toggle() {
  467. for (const item of this._items) {
  468. item.toggle();
  469. }
  470. }
  471. on(event, callback) {
  472. this._events = this._events || {};
  473. this._events[event] = this._events[event] || [];
  474. this._events[event].push(callback);
  475. }
  476. _raise(event, data) {
  477. if (this._events && this._events[event]) {
  478. for (const callback of this._events[event]) {
  479. callback(this, data);
  480. }
  481. }
  482. }
  483. };
  484. sidebar.ArgumentView = class {
  485. constructor(host, argument) {
  486. this._host = host;
  487. this._argument = argument;
  488. this._element = this._host.document.createElement('div');
  489. this._element.className = 'sidebar-view-item-value';
  490. const initializer = argument.initializer;
  491. if (initializer) {
  492. this._element.classList.add('sidebar-view-item-value-dark');
  493. }
  494. const quantization = argument.quantization;
  495. const type = argument.type;
  496. const location = this._argument.location !== undefined;
  497. if (type || initializer || quantization || location) {
  498. this._expander = this._host.document.createElement('div');
  499. this._expander.className = 'sidebar-view-item-value-expander';
  500. this._expander.innerText = '+';
  501. this._expander.addEventListener('click', () => {
  502. this.toggle();
  503. });
  504. this._element.appendChild(this._expander);
  505. }
  506. let name = this._argument.name || '';
  507. this._hasId = name ? true : false;
  508. this._hasKind = initializer && initializer.kind ? true : false;
  509. if (this._hasId || (!this._hasKind && !type)) {
  510. this._hasId = true;
  511. const nameLine = this._host.document.createElement('div');
  512. nameLine.className = 'sidebar-view-item-value-line';
  513. if (typeof name !== 'string') {
  514. throw new Error("Invalid argument identifier '" + JSON.stringify(name) + "'.");
  515. }
  516. name = name.split('\n').shift(); // custom argument id
  517. name = name || ' ';
  518. nameLine.innerHTML = '<span class=\'sidebar-view-item-value-line-content\'>name: <b>' + name + '</b></span>';
  519. this._element.appendChild(nameLine);
  520. }
  521. else if (this._hasKind) {
  522. const kindLine = this._host.document.createElement('div');
  523. kindLine.className = 'sidebar-view-item-value-line';
  524. kindLine.innerHTML = 'kind: <b>' + initializer.kind + '</b>';
  525. this._element.appendChild(kindLine);
  526. }
  527. else if (type) {
  528. const typeLine = this._host.document.createElement('div');
  529. typeLine.className = 'sidebar-view-item-value-line-border';
  530. typeLine.innerHTML = 'type: <code><b>' + type.toString().split('<').join('&lt;').split('>').join('&gt;') + '</b></code>';
  531. this._element.appendChild(typeLine);
  532. }
  533. }
  534. render() {
  535. return this._element;
  536. }
  537. toggle() {
  538. if (this._expander) {
  539. if (this._expander.innerText == '+') {
  540. this._expander.innerText = '-';
  541. const initializer = this._argument.initializer;
  542. if (this._hasId && this._hasKind) {
  543. const kindLine = this._host.document.createElement('div');
  544. kindLine.className = 'sidebar-view-item-value-line-border';
  545. kindLine.innerHTML = 'kind: ' + '<b>' + initializer.kind + '</b>';
  546. this._element.appendChild(kindLine);
  547. }
  548. let type = null;
  549. let denotation = null;
  550. if (this._argument.type) {
  551. type = this._argument.type.toString();
  552. denotation = this._argument.type.denotation || null;
  553. }
  554. if (type && (this._hasId || this._hasKind)) {
  555. const typeLine = this._host.document.createElement('div');
  556. typeLine.className = 'sidebar-view-item-value-line-border';
  557. typeLine.innerHTML = 'type: <code><b>' + type.split('<').join('&lt;').split('>').join('&gt;') + '</b></code>';
  558. this._element.appendChild(typeLine);
  559. }
  560. if (denotation) {
  561. const denotationLine = this._host.document.createElement('div');
  562. denotationLine.className = 'sidebar-view-item-value-line-border';
  563. denotationLine.innerHTML = 'denotation: <code><b>' + denotation + '</b></code>';
  564. this._element.appendChild(denotationLine);
  565. }
  566. const description = this._argument.description;
  567. if (description) {
  568. const descriptionLine = this._host.document.createElement('div');
  569. descriptionLine.className = 'sidebar-view-item-value-line-border';
  570. descriptionLine.innerHTML = description;
  571. this._element.appendChild(descriptionLine);
  572. }
  573. const quantization = this._argument.quantization;
  574. if (quantization) {
  575. const quantizationLine = this._host.document.createElement('div');
  576. quantizationLine.className = 'sidebar-view-item-value-line-border';
  577. const content = !Array.isArray(quantization) ? quantization : '<br><br>' + quantization.map((value) => ' ' + value).join('<br>');
  578. quantizationLine.innerHTML = '<span class=\'sidebar-view-item-value-line-content\'>quantization: ' + '<b>' + content + '</b></span>';
  579. this._element.appendChild(quantizationLine);
  580. }
  581. if (this._argument.location !== undefined) {
  582. const location = this._host.document.createElement('div');
  583. location.className = 'sidebar-view-item-value-line-border';
  584. location.innerHTML = 'location: ' + '<b>' + this._argument.location + '</b>';
  585. this._element.appendChild(location);
  586. }
  587. if (initializer) {
  588. const contentLine = this._host.document.createElement('pre');
  589. const valueLine = this._host.document.createElement('div');
  590. try {
  591. const state = initializer.state;
  592. if (state === null && this._host.save &&
  593. initializer.type.dataType && initializer.type.dataType != '?' &&
  594. initializer.type.shape && initializer.type.shape.dimensions /*&& initializer.type.shape.dimensions.length > 0*/) {
  595. this._saveButton = this._host.document.createElement('div');
  596. this._saveButton.className = 'sidebar-view-item-value-expander';
  597. this._saveButton.innerHTML = '&#x1F4BE;';
  598. this._saveButton.addEventListener('click', () => {
  599. this._raise('export-tensor', initializer);
  600. });
  601. this._element.appendChild(this._saveButton);
  602. }
  603. valueLine.className = 'sidebar-view-item-value-line-border';
  604. contentLine.innerHTML = state || initializer.toString();
  605. }
  606. catch (err) {
  607. contentLine.innerHTML = err.toString();
  608. this._raise('error', err);
  609. }
  610. valueLine.appendChild(contentLine);
  611. this._element.appendChild(valueLine);
  612. }
  613. }
  614. else {
  615. this._expander.innerText = '+';
  616. while (this._element.childElementCount > 2) {
  617. this._element.removeChild(this._element.lastChild);
  618. }
  619. }
  620. }
  621. }
  622. on(event, callback) {
  623. this._events = this._events || {};
  624. this._events[event] = this._events[event] || [];
  625. this._events[event].push(callback);
  626. }
  627. _raise(event, data) {
  628. if (this._events && this._events[event]) {
  629. for (const callback of this._events[event]) {
  630. callback(this, data);
  631. }
  632. }
  633. }
  634. };
  635. sidebar.ModelSidebar = class {
  636. constructor(host, model, graph) {
  637. this._host = host;
  638. this._model = model;
  639. this._elements = [];
  640. if (model.format) {
  641. this._addProperty('format', new sidebar.ValueTextView(this._host, model.format));
  642. }
  643. if (model.producer) {
  644. this._addProperty('producer', new sidebar.ValueTextView(this._host, model.producer));
  645. }
  646. if (model.name) {
  647. this._addProperty('name', new sidebar.ValueTextView(this._host, model.name));
  648. }
  649. if (model.version) {
  650. this._addProperty('version', new sidebar.ValueTextView(this._host, model.version));
  651. }
  652. if (model.description) {
  653. this._addProperty('description', new sidebar.ValueTextView(this._host, model.description));
  654. }
  655. if (model.domain) {
  656. this._addProperty('domain', new sidebar.ValueTextView(this._host, model.domain));
  657. }
  658. if (model.imports) {
  659. this._addProperty('imports', new sidebar.ValueTextView(this._host, model.imports));
  660. }
  661. if (model.runtime) {
  662. this._addProperty('runtime', new sidebar.ValueTextView(this._host, model.runtime));
  663. }
  664. if (model.metadata) {
  665. for (const entry of model.metadata) {
  666. this._addProperty(entry.name, new sidebar.ValueTextView(this._host, entry.value));
  667. }
  668. }
  669. const graphs = Array.isArray(model.graphs) ? model.graphs : [];
  670. if (graphs.length > 1) {
  671. const graphSelector = new sidebar.SelectView(this._host, model.graphs, graph);
  672. graphSelector.on('change', (sender, data) => {
  673. this._raise('update-active-graph', data);
  674. });
  675. this._addProperty('subgraph', graphSelector);
  676. }
  677. if (graph) {
  678. if (graph.version) {
  679. this._addProperty('version', new sidebar.ValueTextView(this._host, graph.version));
  680. }
  681. if (graph.type) {
  682. this._addProperty('type', new sidebar.ValueTextView(this._host, graph.type));
  683. }
  684. if (graph.tags) {
  685. this._addProperty('tags', new sidebar.ValueTextView(this._host, graph.tags));
  686. }
  687. if (graph.description) {
  688. this._addProperty('description', new sidebar.ValueTextView(this._host, graph.description));
  689. }
  690. if (Array.isArray(graph.inputs) && graph.inputs.length > 0) {
  691. this._addHeader('Inputs');
  692. for (const input of graph.inputs) {
  693. this.addArgument(input.name, input);
  694. }
  695. }
  696. if (Array.isArray(graph.outputs) && graph.outputs.length > 0) {
  697. this._addHeader('Outputs');
  698. for (const output of graph.outputs) {
  699. this.addArgument(output.name, output);
  700. }
  701. }
  702. }
  703. const separator = this._host.document.createElement('div');
  704. separator.className = 'sidebar-view-separator';
  705. this._elements.push(separator);
  706. }
  707. render() {
  708. return this._elements;
  709. }
  710. _addHeader(title) {
  711. const headerElement = this._host.document.createElement('div');
  712. headerElement.className = 'sidebar-view-header';
  713. headerElement.innerText = title;
  714. this._elements.push(headerElement);
  715. }
  716. _addProperty(name, value) {
  717. const item = new sidebar.NameValueView(this._host, name, value);
  718. this._elements.push(item.render());
  719. }
  720. addArgument(name, argument) {
  721. const view = new sidebar.ParameterView(this._host, argument);
  722. view.toggle();
  723. const item = new sidebar.NameValueView(this._host, name, view);
  724. this._elements.push(item.render());
  725. }
  726. on(event, callback) {
  727. this._events = this._events || {};
  728. this._events[event] = this._events[event] || [];
  729. this._events[event].push(callback);
  730. }
  731. _raise(event, data) {
  732. if (this._events && this._events[event]) {
  733. for (const callback of this._events[event]) {
  734. callback(this, data);
  735. }
  736. }
  737. }
  738. };
  739. sidebar.DocumentationSidebar = class {
  740. constructor(host, metadata) {
  741. this._host = host;
  742. this._metadata = metadata;
  743. }
  744. render() {
  745. if (!this._elements) {
  746. this._elements = [];
  747. const type = sidebar.DocumentationSidebar.formatDocumentation(this._metadata);
  748. const element = this._host.document.createElement('div');
  749. element.setAttribute('class', 'sidebar-view-documentation');
  750. this._append(element, 'h1', type.name);
  751. if (type.summary) {
  752. this._append(element, 'p', type.summary);
  753. }
  754. if (type.description) {
  755. this._append(element, 'p', type.description);
  756. }
  757. if (Array.isArray(type.attributes) && type.attributes.length > 0) {
  758. this._append(element, 'h2', 'Attributes');
  759. const attributes = this._append(element, 'dl');
  760. for (const attribute of type.attributes) {
  761. this._append(attributes, 'dt', attribute.name + (attribute.type ? ': <tt>' + attribute.type + '</tt>' : ''));
  762. this._append(attributes, 'dd', attribute.description);
  763. }
  764. element.appendChild(attributes);
  765. }
  766. if (Array.isArray(type.inputs) && type.inputs.length > 0) {
  767. this._append(element, 'h2', 'Inputs' + (type.inputs_range ? ' (' + type.inputs_range + ')' : ''));
  768. const inputs = this._append(element, 'dl');
  769. for (const input of type.inputs) {
  770. this._append(inputs, 'dt', input.name + (input.type ? ': <tt>' + input.type + '</tt>' : '') + (input.option ? ' (' + input.option + ')' : ''));
  771. this._append(inputs, 'dd', input.description);
  772. }
  773. }
  774. if (Array.isArray(type.outputs) && type.outputs.length > 0) {
  775. this._append(element, 'h2', 'Outputs' + (type.outputs_range ? ' (' + type.outputs_range + ')' : ''));
  776. const outputs = this._append(element, 'dl');
  777. for (const output of type.outputs) {
  778. this._append(outputs, 'dt', output.name + (output.type ? ': <tt>' + output.type + '</tt>' : '') + (output.option ? ' (' + output.option + ')' : ''));
  779. this._append(outputs, 'dd', output.description);
  780. }
  781. }
  782. if (Array.isArray(type.type_constraints) && type.type_constraints.length > 0) {
  783. this._append(element, 'h2', 'Type Constraints');
  784. const type_constraints = this._append(element, 'dl');
  785. for (const type_constraint of type.type_constraints) {
  786. this._append(type_constraints, 'dt', type_constraint.type_param_str + ': ' + type_constraint.allowed_type_strs.map((item) => '<tt>' + item + '</tt>').join(', '));
  787. this._append(type_constraints, 'dd', type_constraint.description);
  788. }
  789. }
  790. if (Array.isArray(type.examples) && type.examples.length > 0) {
  791. this._append(element, 'h2', 'Examples');
  792. for (const example of type.examples) {
  793. this._append(element, 'h3', example.summary);
  794. this._append(element, 'pre', example.code);
  795. }
  796. }
  797. if (Array.isArray(type.references) && type.references.length > 0) {
  798. this._append(element, 'h2', 'References');
  799. const references = this._append(element, 'ul');
  800. for (const reference of type.references) {
  801. this._append(references, 'li', reference.description);
  802. }
  803. }
  804. if (type.domain && type.version && type.support_level) {
  805. this._append(element, 'h2', 'Support');
  806. this._append(element, 'dl', 'In domain <tt>' + type.domain + '</tt> since version <tt>' + type.version + '</tt> at support level <tt>' + type.support_level + '</tt>.');
  807. }
  808. if (!this._host.type !== 'Electron') {
  809. element.addEventListener('click', (e) => {
  810. if (e.target && e.target.href) {
  811. const link = e.target.href;
  812. if (link.startsWith('http://') || link.startsWith('https://')) {
  813. e.preventDefault();
  814. this._raise('navigate', { link: link });
  815. }
  816. }
  817. });
  818. }
  819. this._elements = [ element ];
  820. const separator = this._host.document.createElement('div');
  821. separator.className = 'sidebar-view-separator';
  822. this._elements.push(separator);
  823. }
  824. return this._elements;
  825. }
  826. on(event, callback) {
  827. this._events = this._events || {};
  828. this._events[event] = this._events[event] || [];
  829. this._events[event].push(callback);
  830. }
  831. _raise(event, data) {
  832. if (this._events && this._events[event]) {
  833. for (const callback of this._events[event]) {
  834. callback(this, data);
  835. }
  836. }
  837. }
  838. _append(parent, type, content) {
  839. const element = this._host.document.createElement(type);
  840. if (content) {
  841. element.innerHTML = content;
  842. }
  843. parent.appendChild(element);
  844. return element;
  845. }
  846. static formatDocumentation(source) {
  847. if (source) {
  848. const generator = new markdown.Generator();
  849. const target = {};
  850. if (source.name !== undefined) {
  851. target.name = source.name;
  852. }
  853. if (source.module !== undefined) {
  854. target.module = source.module;
  855. }
  856. if (source.category !== undefined) {
  857. target.category = source.category;
  858. }
  859. if (source.summary !== undefined) {
  860. target.summary = generator.html(source.summary);
  861. }
  862. if (source.description !== undefined) {
  863. target.description = generator.html(source.description);
  864. }
  865. if (Array.isArray(source.attributes)) {
  866. target.attributes = source.attributes.map((source) => {
  867. const target = {};
  868. target.name = source.name;
  869. if (source.type !== undefined) {
  870. target.type = source.type;
  871. }
  872. if (source.option !== undefined) {
  873. target.option = source.option;
  874. }
  875. if (source.optional !== undefined) {
  876. target.optional = source.optional;
  877. }
  878. if (source.required !== undefined) {
  879. target.required = source.required;
  880. }
  881. if (source.minimum !== undefined) {
  882. target.minimum = source.minimum;
  883. }
  884. if (source.src !== undefined) {
  885. target.src = source.src;
  886. }
  887. if (source.src_type !== undefined) {
  888. target.src_type = source.src_type;
  889. }
  890. if (source.description !== undefined) {
  891. target.description = generator.html(source.description);
  892. }
  893. if (source.default !== undefined) {
  894. target.default = source.default;
  895. }
  896. if (source.visible !== undefined) {
  897. target.visible = source.visible;
  898. }
  899. return target;
  900. });
  901. }
  902. if (Array.isArray(source.inputs)) {
  903. target.inputs = source.inputs.map((source) => {
  904. const target = {};
  905. target.name = source.name;
  906. if (source.type !== undefined) {
  907. target.type = source.type;
  908. }
  909. if (source.description !== undefined) {
  910. target.description = generator.html(source.description);
  911. }
  912. if (source.default !== undefined) {
  913. target.default = source.default;
  914. }
  915. if (source.src !== undefined) {
  916. target.src = source.src;
  917. }
  918. if (source.list !== undefined) {
  919. target.list = source.list;
  920. }
  921. if (source.isRef !== undefined) {
  922. target.isRef = source.isRef;
  923. }
  924. if (source.typeAttr !== undefined) {
  925. target.typeAttr = source.typeAttr;
  926. }
  927. if (source.numberAttr !== undefined) {
  928. target.numberAttr = source.numberAttr;
  929. }
  930. if (source.typeListAttr !== undefined) {
  931. target.typeListAttr = source.typeListAttr;
  932. }
  933. if (source.option !== undefined) {
  934. target.option = source.option;
  935. }
  936. if (source.optional !== undefined) {
  937. target.optional = source.optional;
  938. }
  939. if (source.visible !== undefined) {
  940. target.visible = source.visible;
  941. }
  942. return target;
  943. });
  944. }
  945. if (Array.isArray(source.outputs)) {
  946. target.outputs = source.outputs.map((source) => {
  947. const target = {};
  948. target.name = source.name;
  949. if (source.type) {
  950. target.type = source.type;
  951. }
  952. if (source.description !== undefined) {
  953. target.description = generator.html(source.description);
  954. }
  955. if (source.list !== undefined) {
  956. target.list = source.list;
  957. }
  958. if (source.typeAttr !== undefined) {
  959. target.typeAttr = source.typeAttr;
  960. }
  961. if (source.typeListAttr !== undefined) {
  962. target.typeListAttr = source.typeAttr;
  963. }
  964. if (source.numberAttr !== undefined) {
  965. target.numberAttr = source.numberAttr;
  966. }
  967. if (source.isRef !== undefined) {
  968. target.isRef = source.isRef;
  969. }
  970. if (source.option !== undefined) {
  971. target.option = source.option;
  972. }
  973. return target;
  974. });
  975. }
  976. if (Array.isArray(source.references)) {
  977. target.references = source.references.map((source) => {
  978. if (source) {
  979. target.description = generator.html(source.description);
  980. }
  981. return target;
  982. });
  983. }
  984. if (source.version !== undefined) {
  985. target.version = source.version;
  986. }
  987. if (source.operator !== undefined) {
  988. target.operator = source.operator;
  989. }
  990. if (source.identifier !== undefined) {
  991. target.identifier = source.identifier;
  992. }
  993. if (source.package !== undefined) {
  994. target.package = source.package;
  995. }
  996. if (source.support_level !== undefined) {
  997. target.support_level = source.support_level;
  998. }
  999. if (source.min_input !== undefined) {
  1000. target.min_input = source.min_input;
  1001. }
  1002. if (source.max_input !== undefined) {
  1003. target.max_input = source.max_input;
  1004. }
  1005. if (source.min_output !== undefined) {
  1006. target.min_output = source.min_output;
  1007. }
  1008. if (source.max_input !== undefined) {
  1009. target.max_output = source.max_output;
  1010. }
  1011. if (source.inputs_range !== undefined) {
  1012. target.inputs_range = source.inputs_range;
  1013. }
  1014. if (source.outputs_range !== undefined) {
  1015. target.outputs_range = source.outputs_range;
  1016. }
  1017. if (source.examples !== undefined) {
  1018. target.examples = source.examples;
  1019. }
  1020. if (source.constants !== undefined) {
  1021. target.constants = source.constants;
  1022. }
  1023. if (source.type_constraints !== undefined) {
  1024. target.type_constraints = source.type_constraints;
  1025. }
  1026. return target;
  1027. }
  1028. return '';
  1029. }
  1030. };
  1031. sidebar.FindSidebar = class {
  1032. constructor(host, element, graph) {
  1033. this._host = host;
  1034. this._graphElement = element;
  1035. this._graph = graph;
  1036. this._contentElement = this._host.document.createElement('div');
  1037. this._contentElement.setAttribute('class', 'sidebar-view-find');
  1038. this._searchElement = this._host.document.createElement('input');
  1039. this._searchElement.setAttribute('id', 'search');
  1040. this._searchElement.setAttribute('type', 'text');
  1041. this._searchElement.setAttribute('spellcheck', 'false');
  1042. this._searchElement.setAttribute('placeholder', 'Search...');
  1043. this._searchElement.setAttribute('style', 'width: 100%');
  1044. this._searchElement.addEventListener('input', (e) => {
  1045. this.update(e.target.value);
  1046. this._raise('search-text-changed', e.target.value);
  1047. });
  1048. this._resultElement = this._host.document.createElement('ol');
  1049. this._resultElement.addEventListener('click', (e) => {
  1050. this.select(e);
  1051. });
  1052. this._contentElement.appendChild(this._searchElement);
  1053. this._contentElement.appendChild(this._resultElement);
  1054. }
  1055. on(event, callback) {
  1056. this._events = this._events || {};
  1057. this._events[event] = this._events[event] || [];
  1058. this._events[event].push(callback);
  1059. }
  1060. _raise(event, data) {
  1061. if (this._events && this._events[event]) {
  1062. for (const callback of this._events[event]) {
  1063. callback(this, data);
  1064. }
  1065. }
  1066. }
  1067. select(e) {
  1068. const selection = [];
  1069. const id = e.target.id;
  1070. const nodesElement = this._graphElement.getElementById('nodes');
  1071. let nodeElement = nodesElement.firstChild;
  1072. while (nodeElement) {
  1073. if (nodeElement.id == id) {
  1074. selection.push(nodeElement);
  1075. }
  1076. nodeElement = nodeElement.nextSibling;
  1077. }
  1078. const edgePathsElement = this._graphElement.getElementById('edge-paths');
  1079. let edgePathElement = edgePathsElement.firstChild;
  1080. while (edgePathElement) {
  1081. if (edgePathElement.id == id) {
  1082. selection.push(edgePathElement);
  1083. }
  1084. edgePathElement = edgePathElement.nextSibling;
  1085. }
  1086. let initializerElement = this._graphElement.getElementById(id);
  1087. if (initializerElement) {
  1088. while (initializerElement.parentElement) {
  1089. initializerElement = initializerElement.parentElement;
  1090. if (initializerElement.id && initializerElement.id.startsWith('node-')) {
  1091. selection.push(initializerElement);
  1092. break;
  1093. }
  1094. }
  1095. }
  1096. if (selection.length > 0) {
  1097. this._raise('select', selection);
  1098. }
  1099. }
  1100. focus(searchText) {
  1101. this._searchElement.focus();
  1102. this._searchElement.value = '';
  1103. this._searchElement.value = searchText;
  1104. this.update(searchText);
  1105. }
  1106. update(searchText) {
  1107. while (this._resultElement.lastChild) {
  1108. this._resultElement.removeChild(this._resultElement.lastChild);
  1109. }
  1110. let terms = null;
  1111. let callback = null;
  1112. const unquote = searchText.match(new RegExp(/^'(.*)'|"(.*)"$/));
  1113. if (unquote) {
  1114. const term = unquote[1] || unquote[2];
  1115. terms = [ term ];
  1116. callback = (name) => {
  1117. return term == name;
  1118. };
  1119. }
  1120. else {
  1121. terms = searchText.trim().toLowerCase().split(' ').map((term) => term.trim()).filter((term) => term.length > 0);
  1122. callback = (name) => {
  1123. return terms.every((term) => name.toLowerCase().indexOf(term) !== -1);
  1124. };
  1125. }
  1126. const nodes = new Set();
  1127. const edges = new Set();
  1128. for (const node of this._graph.nodes.values()) {
  1129. const label = node.label;
  1130. const initializers = [];
  1131. if (label.class === 'graph-node' || label.class === 'graph-input') {
  1132. for (const input of label.inputs) {
  1133. for (const argument of input.arguments) {
  1134. if (argument.name && !edges.has(argument.name)) {
  1135. const match = (argument, term) => {
  1136. if (argument.name && argument.name.toLowerCase().indexOf(term) !== -1) {
  1137. return true;
  1138. }
  1139. if (argument.type) {
  1140. if (argument.type.dataType && term === argument.type.dataType.toLowerCase()) {
  1141. return true;
  1142. }
  1143. if (argument.type.shape) {
  1144. if (term === argument.type.shape.toString().toLowerCase()) {
  1145. return true;
  1146. }
  1147. if (argument.type.shape && Array.isArray(argument.type.shape.dimensions)) {
  1148. const dimensions = argument.type.shape.dimensions.map((dimension) => dimension ? dimension.toString().toLowerCase() : '');
  1149. if (term === dimensions.join(',')) {
  1150. return true;
  1151. }
  1152. if (dimensions.some((dimension) => term === dimension)) {
  1153. return true;
  1154. }
  1155. }
  1156. }
  1157. }
  1158. return false;
  1159. };
  1160. if (terms.every((term) => match(argument, term))) {
  1161. if (!argument.initializer) {
  1162. const inputItem = this._host.document.createElement('li');
  1163. inputItem.innerText = '\u2192 ' + argument.name.split('\n').shift(); // custom argument id
  1164. inputItem.id = 'edge-' + argument.name;
  1165. this._resultElement.appendChild(inputItem);
  1166. edges.add(argument.name);
  1167. }
  1168. else {
  1169. initializers.push(argument);
  1170. }
  1171. }
  1172. }
  1173. }
  1174. }
  1175. }
  1176. if (label.class === 'graph-node') {
  1177. const name = label.value.name;
  1178. const type = label.value.type.name;
  1179. if (!nodes.has(label.id) &&
  1180. ((name && callback(name) || (type && callback(type))))) {
  1181. const nameItem = this._host.document.createElement('li');
  1182. nameItem.innerText = '\u25A2 ' + (name || '[' + type + ']');
  1183. nameItem.id = label.id;
  1184. this._resultElement.appendChild(nameItem);
  1185. nodes.add(label.id);
  1186. }
  1187. }
  1188. for (const argument of initializers) {
  1189. if (argument.name) {
  1190. const initializeItem = this._host.document.createElement('li');
  1191. initializeItem.innerText = '\u25A0 ' + argument.name.split('\n').shift(); // custom argument id
  1192. initializeItem.id = 'initializer-' + argument.name;
  1193. this._resultElement.appendChild(initializeItem);
  1194. }
  1195. }
  1196. }
  1197. for (const node of this._graph.nodes.values()) {
  1198. const label = node.label;
  1199. if (label.class === 'graph-node' || label.class === 'graph-output') {
  1200. for (const output of label.outputs) {
  1201. for (const argument of output.arguments) {
  1202. if (argument.name && !edges.has(argument.name) && terms.every((term) => argument.name.toLowerCase().indexOf(term) != -1)) {
  1203. const outputItem = this._host.document.createElement('li');
  1204. outputItem.innerText = '\u2192 ' + argument.name.split('\n').shift(); // custom argument id
  1205. outputItem.id = 'edge-' + argument.name;
  1206. this._resultElement.appendChild(outputItem);
  1207. edges.add(argument.name);
  1208. }
  1209. }
  1210. }
  1211. }
  1212. }
  1213. this._resultElement.style.display = this._resultElement.childNodes.length != 0 ? 'block' : 'none';
  1214. }
  1215. get content() {
  1216. return this._contentElement;
  1217. }
  1218. };
  1219. sidebar.Formatter = class {
  1220. constructor(value, type, quote) {
  1221. this._value = value;
  1222. this._type = type;
  1223. this._quote = quote;
  1224. this._values = new Set();
  1225. }
  1226. toString() {
  1227. return this._format(this._value, this._type, this._quote);
  1228. }
  1229. _format(value, type, quote) {
  1230. if (value && value.__class__ && value.__class__.__module__ === 'builtins' && value.__class__.__name__ === 'type') {
  1231. return value.__module__ + '.' + value.__name__;
  1232. }
  1233. if (value && value.__class__ && value.__class__.__module__ === 'builtins' && value.__class__.__name__ === 'function') {
  1234. return value.__module__ + '.' + value.__name__;
  1235. }
  1236. if (typeof value === 'function') {
  1237. return value();
  1238. }
  1239. if (value && (value instanceof base.Int64 || value instanceof base.Uint64)) {
  1240. return value.toString();
  1241. }
  1242. if (Number.isNaN(value)) {
  1243. return 'NaN';
  1244. }
  1245. switch (type) {
  1246. case 'shape':
  1247. return value ? value.toString() : '(null)';
  1248. case 'shape[]':
  1249. if (value && !Array.isArray(value)) {
  1250. throw new Error("Invalid shape '" + JSON.stringify(value) + "'.");
  1251. }
  1252. return value ? value.map((item) => item.toString()).join(', ') : '(null)';
  1253. case 'graph':
  1254. return value ? value.name : '(null)';
  1255. case 'graph[]':
  1256. return value ? value.map((graph) => graph.name).join(', ') : '(null)';
  1257. case 'tensor':
  1258. if (value && value.type && value.type.shape && value.type.shape.dimensions && value.type.shape.dimensions.length == 0) {
  1259. return value.toString();
  1260. }
  1261. return '[...]';
  1262. case 'function':
  1263. return value.type.name;
  1264. case 'function[]':
  1265. return value ? value.map((item) => item.type.name).join(', ') : '(null)';
  1266. case 'type':
  1267. return value ? value.toString() : '(null)';
  1268. case 'type[]':
  1269. return value ? value.map((item) => item.toString()).join(', ') : '(null)';
  1270. default:
  1271. break;
  1272. }
  1273. if (typeof value === 'string' && (!type || type != 'string')) {
  1274. return quote ? '"' + value + '"' : value;
  1275. }
  1276. if (Array.isArray(value)) {
  1277. if (value.length == 0) {
  1278. return quote ? '[]' : '';
  1279. }
  1280. let ellipsis = false;
  1281. if (value.length > 1000) {
  1282. value = value.slice(0, 1000);
  1283. ellipsis = true;
  1284. }
  1285. const itemType = (type && type.endsWith('[]')) ? type.substring(0, type.length - 2) : null;
  1286. const array = value.map((item) => {
  1287. if (item && (item instanceof base.Int64 || item instanceof base.Uint64)) {
  1288. return item.toString();
  1289. }
  1290. if (Number.isNaN(item)) {
  1291. return 'NaN';
  1292. }
  1293. const quote = !itemType || itemType === 'string';
  1294. return this._format(item, itemType, quote);
  1295. });
  1296. if (ellipsis) {
  1297. array.push('\u2026');
  1298. }
  1299. return quote ? [ '[', array.join(', '), ']' ].join(' ') : array.join(', ');
  1300. }
  1301. if (value === null) {
  1302. return quote ? 'null' : '';
  1303. }
  1304. if (value === undefined) {
  1305. return 'undefined';
  1306. }
  1307. if (value !== Object(value)) {
  1308. return value.toString();
  1309. }
  1310. if (this._values.has(value)) {
  1311. return '\u2026';
  1312. }
  1313. this._values.add(value);
  1314. let list = null;
  1315. const entries = Object.entries(value).filter((entry) => !entry[0].startsWith('__') && !entry[0].endsWith('__'));
  1316. if (entries.length == 1) {
  1317. list = [ this._format(entries[0][1], null, true) ];
  1318. }
  1319. else {
  1320. list = new Array(entries.length);
  1321. for (let i = 0; i < entries.length; i++) {
  1322. const entry = entries[i];
  1323. list[i] = entry[0] + ': ' + this._format(entry[1], null, true);
  1324. }
  1325. }
  1326. let objectType = value.__type__;
  1327. if (!objectType && value.constructor.name && value.constructor.name !== 'Object') {
  1328. objectType = value.constructor.name;
  1329. }
  1330. if (objectType) {
  1331. return objectType + (list.length == 0 ? '()' : [ '(', list.join(', '), ')' ].join(''));
  1332. }
  1333. switch (list.length) {
  1334. case 0:
  1335. return quote ? '()' : '';
  1336. case 1:
  1337. return list[0];
  1338. default:
  1339. return quote ? [ '(', list.join(', '), ')' ].join(' ') : list.join(', ');
  1340. }
  1341. }
  1342. };
  1343. const markdown = {};
  1344. markdown.Generator = class {
  1345. constructor() {
  1346. this._newlineRegExp = /^\n+/;
  1347. this._codeRegExp = /^( {4}[^\n]+\n*)+/;
  1348. this._fencesRegExp = /^ {0,3}(`{3,}(?=[^`\n]*\n)|~{3,})([^\n]*)\n(?:|([\s\S]*?)\n)(?: {0,3}\1[~`]* *(?:\n+|$)|$)/;
  1349. this._hrRegExp = /^ {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)/;
  1350. this._headingRegExp = /^ {0,3}(#{1,6}) +([^\n]*?)(?: +#+)? *(?:\n+|$)/;
  1351. this._blockquoteRegExp = /^( {0,3}> ?(([^\n]+(?:\n(?! {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)| {0,3}#{1,6} | {0,3}>| {0,3}(?:`{3,}(?=[^`\n]*\n)|~{3,})[^\n]*\n| {0,3}(?:[*+-]|1[.)]) |<\/?(?:address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)(?: +|\n|\/?>)|<(?:script|pre|style|!--))[^\n]+)*)|[^\n]*)(?:\n|$))+/;
  1352. this._listRegExp = /^( {0,3})((?:[*+-]|\d{1,9}[.)])) [\s\S]+?(?:\n+(?=\1?(?:(?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$))|\n+(?= {0,3}\[((?!\s*\])(?:\\[[\]]|[^[\]])+)\]: *\n? *<?([^\s>]+)>?(?:(?: +\n? *| *\n *)((?:"(?:\\"?|[^"\\])*"|'[^'\n]*(?:\n[^'\n]+)*\n?'|\([^()]*\))))? *(?:\n+|$))|\n{2,}(?! )(?!\1(?:[*+-]|\d{1,9}[.)]) )\n*|\s*$)/;
  1353. this._htmlRegExp = /^ {0,3}(?:<(script|pre|style)[\s>][\s\S]*?(?:<\/\1>[^\n]*\n+|$)|<!--(?!-?>)[\s\S]*?(?:-->|$)[^\n]*(\n+|$)|<\?[\s\S]*?(?:\?>\n*|$)|<![A-Z][\s\S]*?(?:>\n*|$)|<!\[CDATA\[[\s\S]*?(?:\]\]>\n*|$)|<\/?(address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)(?: +|\n|\/?>)[\s\S]*?(?:\n{2,}|$)|<(?!script|pre|style)([a-z][\w-]*)(?: +[a-zA-Z:_][\w.:-]*(?: *= *"[^"\n]*"| *= *'[^'\n]*'| *= *[^\s"'=<>`]+)?)*? *\/?>(?=[ \t]*(?:\n|$))[\s\S]*?(?:\n{2,}|$)|<\/(?!script|pre|style)[a-z][\w-]*\s*>(?=[ \t]*(?:\n|$))[\s\S]*?(?:\n{2,}|$))/i;
  1354. this._defRegExp = /^ {0,3}\[((?!\s*\])(?:\\[[\]]|[^[\]])+)\]: *\n? *<?([^\s>]+)>?(?:(?: +\n? *| *\n *)((?:"(?:\\"?|[^"\\])*"|'[^'\n]*(?:\n[^'\n]+)*\n?'|\([^()]*\))))? *(?:\n+|$)/;
  1355. this._nptableRegExp = /^ *([^|\n ].*\|.*)\n {0,3}([-:]+ *\|[-| :]*)(?:\n((?:(?!\n| {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)| {0,3}#{1,6} | {0,3}>| {4}[^\n]| {0,3}(?:`{3,}(?=[^`\n]*\n)|~{3,})[^\n]*\n| {0,3}(?:[*+-]|1[.)]) |<\/?(?:address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)(?: +|\n|\/?>)|<(?:script|pre|style|!--)).*(?:\n|$))*)\n*|$)/;
  1356. this._tableRegExp = /^ *\|(.+)\n {0,3}\|?( *[-:]+[-| :]*)(?:\n *((?:(?!\n| {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)| {0,3}#{1,6} | {0,3}>| {4}[^\n]| {0,3}(?:`{3,}(?=[^`\n]*\n)|~{3,})[^\n]*\n| {0,3}(?:[*+-]|1[.)]) |<\/?(?:address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)(?: +|\n|\/?>)|<(?:script|pre|style|!--)).*(?:\n|$))*)\n*|$)/;
  1357. this._lheadingRegExp = /^([^\n]+)\n {0,3}(=+|-+) *(?:\n+|$)/;
  1358. this._textRegExp = /^[^\n]+/;
  1359. this._bulletRegExp = /(?:[*+-]|\d{1,9}[.)])/;
  1360. this._itemRegExp = /^( *)((?:[*+-]|\d{1,9}[.)])) ?[^\n]*(?:\n(?!\1(?:[*+-]|\d{1,9}[.)]) ?)[^\n]*)*/gm;
  1361. this._paragraphRegExp = /^([^\n]+(?:\n(?! {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)| {0,3}#{1,6} | {0,3}>| {0,3}(?:`{3,}(?=[^`\n]*\n)|~{3,})[^\n]*\n| {0,3}(?:[*+-]|1[.)]) |<\/?(?:address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)(?: +|\n|\/?>)|<(?:script|pre|style|!--))[^\n]+)*)/;
  1362. this._backpedalRegExp = /(?:[^?!.,:;*_~()&]+|\([^)]*\)|&(?![a-zA-Z0-9]+;$)|[?!.,:;*_~)]+(?!$))+/;
  1363. this._escapeRegExp = /^\\([!"#$%&'()*+,\-./:;<=>?@[\]\\^_`{|}~~|])/;
  1364. this._escapesRegExp = /\\([!"#$%&'()*+,\-./:;<=>?@[\]\\^_`{|}~])/g;
  1365. /* eslint-disable no-control-regex */
  1366. this._autolinkRegExp = /^<([a-zA-Z][a-zA-Z0-9+.-]{1,31}:[^\s\x00-\x1f<>]*|[a-zA-Z0-9.!#$%&'*+/=?_`{|}~-]+(@)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+(?![-_]))>/;
  1367. this._linkRegExp = /^!?\[((?:\[(?:\\.|[^[\]\\])*\]|\\.|`[^`]*`|[^[\]\\`])*?)\]\(\s*(<(?:\\[<>]?|[^\s<>\\])*>|[^\s\x00-\x1f]*)(?:\s+("(?:\\"?|[^"\\])*"|'(?:\\'?|[^'\\])*'|\((?:\\\)?|[^)\\])*\)))?\s*\)/;
  1368. /* eslint-enable no-control-regex */
  1369. this._urlRegExp = /^((?:ftp|https?):\/\/|www\.)(?:[a-zA-Z0-9-]+\.?)+[^\s<]*|^[A-Za-z0-9._+-]+(@)[a-zA-Z0-9-_]+(?:\.[a-zA-Z0-9-_]*[a-zA-Z0-9])+(?![-_])/i;
  1370. this._tagRegExp = /^<!--(?!-?>)[\s\S]*?-->|^<\/[a-zA-Z][\w:-]*\s*>|^<[a-zA-Z][\w-]*(?:\s+[a-zA-Z:_][\w.:-]*(?:\s*=\s*"[^"]*"|\s*=\s*'[^']*'|\s*=\s*[^\s"'=<>`]+)?)*?\s*\/?>|^<\?[\s\S]*?\?>|^<![a-zA-Z]+\s[\s\S]*?>|^<!\[CDATA\[[\s\S]*?\]\]>/;
  1371. this._reflinkRegExp = /^!?\[((?:\[(?:\\.|[^[\]\\])*\]|\\.|`[^`]*`|[^[\]\\`])*?)\]\[(?!\s*\])((?:\\[[\]]?|[^[\]\\])+)\]/;
  1372. this._nolinkRegExp = /^!?\[(?!\s*\])((?:\[[^[\]]*\]|\\[[\]]|[^[\]])*)\](?:\[\])?/;
  1373. this._reflinkSearchRegExp = /!?\[((?:\[(?:\\.|[^[\]\\])*\]|\\.|`[^`]*`|[^[\]\\`])*?)\]\[(?!\s*\])((?:\\[[\]]?|[^[\]\\])+)\]|!?\[(?!\s*\])((?:\[[^[\]]*\]|\\[[\]]|[^[\]])*)\](?:\[\])?(?!\()/g;
  1374. this._strongStartRegExp = /^(?:(\*\*(?=[*!"#$%&'()+\-.,/:;<=>?@[\]`{|}~]))|\*\*)(?![\s])|__/;
  1375. this._strongMiddleRegExp = /^\*\*(?:(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^*]|\\\*)|__[^_]*?__|\*\*\[^\*\]*?\*\*)|\*(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^*]|\\\*)|__[^_]*?__|\*\*\[^\*\]*?\*\*)*?\*)+?\*\*$|^__(?![\s])((?:(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^_]|\\_)|__[^_]*?__|\*\*\[^\*\]*?\*\*)|_(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^_]|\\_)|__[^_]*?__|\*\*\[^\*\]*?\*\*)*?_)+?)__$/;
  1376. this._strongEndAstRegExp = /[^!"#$%&'()+\-.,/:;<=>?@[\]`{|}~\s]\*\*(?!\*)|[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~]\*\*(?!\*)(?:(?=[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~_\s]|$))/g;
  1377. this._strongEndUndRegExp = /[^\s]__(?!_)(?:(?=[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~*\s])|$)/g;
  1378. this._emStartRegExp = /^(?:(\*(?=[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~]))|\*)(?![*\s])|_/;
  1379. this._emMiddleRegExp = /^\*(?:(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^*]|\\\*)|__[^_]*?__|\*\*\[^\*\]*?\*\*)|\*(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^*]|\\\*)|__[^_]*?__|\*\*\[^\*\]*?\*\*)*?\*)+?\*$|^_(?![_\s])(?:(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^_]|\\_)|__[^_]*?__|\*\*\[^\*\]*?\*\*)|_(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^_]|\\_)|__[^_]*?__|\*\*\[^\*\]*?\*\*)*?_)+?_$/;
  1380. this._emEndAstRegExp = /[^!"#$%&'()+\-.,/:;<=>?@[\]`{|}~\s]\*(?!\*)|[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~]\*(?!\*)(?:(?=[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~_\s]|$))/g;
  1381. this._emEndUndRegExp = /[^\s]_(?!_)(?:(?=[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~*\s])|$)/g,
  1382. this._codespanRegExp = /^(`+)([^`]|[^`][\s\S]*?[^`])\1(?!`)/;
  1383. this._brRegExp = /^( {2,}|\\)\n(?!\s*$)/;
  1384. this._delRegExp = /^~+(?=\S)([\s\S]*?\S)~+/;
  1385. this._textspanRegExp = /^(`+|[^`])(?:(?= {2,}\n)|[\s\S]*?(?:(?=[\\<![`*~]|\b_|https?:\/\/|ftp:\/\/|www\.|$)|[^ ](?= {2,}\n)|[^a-zA-Z0-9.!#$%&'*+/=?_`{|}~-](?=[a-zA-Z0-9.!#$%&'*+/=?_`{|}~-]+@))|(?=[a-zA-Z0-9.!#$%&'*+/=?_`{|}~-]+@))/;
  1386. this._punctuationRegExp = /^([\s*!"#$%&'()+\-.,/:;<=>?@[\]`{|}~])/;
  1387. this._blockSkipRegExp = /\[[^\]]*?\]\([^)]*?\)|`[^`]*?`|<[^>]*?>/g;
  1388. this._escapeTestRegExp = /[&<>"']/;
  1389. this._escapeReplaceRegExp = /[&<>"']/g;
  1390. this._escapeTestNoEncodeRegExp = /[<>"']|&(?!#?\w+;)/;
  1391. this._escapeReplaceNoEncodeRegExp = /[<>"']|&(?!#?\w+;)/g;
  1392. this._escapeReplacementsMap = { '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' };
  1393. }
  1394. html(source) {
  1395. const tokens = [];
  1396. const links = new Map();
  1397. this._tokenize(source.replace(/\r\n|\r/g, '\n').replace(/\t/g, ' '), tokens, links, true);
  1398. this._tokenizeBlock(tokens, links);
  1399. const slugs = new Map();
  1400. const result = this._render(tokens, slugs, true);
  1401. return result;
  1402. }
  1403. _tokenize(source, tokens, links, top) {
  1404. source = source.replace(/^ +$/gm, '');
  1405. while (source) {
  1406. let match = this._newlineRegExp.exec(source);
  1407. if (match) {
  1408. source = source.substring(match[0].length);
  1409. if (match[0].length > 1) {
  1410. tokens.push({ type: 'space' });
  1411. }
  1412. continue;
  1413. }
  1414. match = this._codeRegExp.exec(source);
  1415. if (match) {
  1416. source = source.substring(match[0].length);
  1417. const lastToken = tokens[tokens.length - 1];
  1418. if (lastToken && lastToken.type === 'paragraph') {
  1419. lastToken.text += '\n' + match[0].trimRight();
  1420. }
  1421. else {
  1422. const text = match[0].replace(/^ {4}/gm, '').replace(/\n*$/, '');
  1423. tokens.push({ type: 'code', text: text });
  1424. }
  1425. continue;
  1426. }
  1427. match = this._fencesRegExp.exec(source);
  1428. if (match) {
  1429. source = source.substring(match[0].length);
  1430. const language = match[2] ? match[2].trim() : match[2];
  1431. let content = match[3] || '';
  1432. const matchIndent = match[0].match(/^(\s+)(?:```)/);
  1433. if (matchIndent !== null) {
  1434. const indent = matchIndent[1];
  1435. content = content.split('\n').map(node => {
  1436. const match = node.match(/^\s+/);
  1437. return (match !== null && match[0].length >= indent.length) ? node.slice(indent.length) : node;
  1438. }).join('\n');
  1439. }
  1440. tokens.push({ type: 'code', language: language, text: content });
  1441. continue;
  1442. }
  1443. match = this._headingRegExp.exec(source);
  1444. if (match) {
  1445. source = source.substring(match[0].length);
  1446. tokens.push({ type: 'heading', depth: match[1].length, text: match[2] });
  1447. continue;
  1448. }
  1449. match = this._nptableRegExp.exec(source);
  1450. if (match) {
  1451. const header = this._splitCells(match[1].replace(/^ *| *\| *$/g, ''));
  1452. const align = match[2].replace(/^ *|\| *$/g, '').split(/ *\| */);
  1453. if (header.length === align.length) {
  1454. const cells = match[3] ? match[3].replace(/\n$/, '').split('\n') : [];
  1455. const token = { type: 'table', header: header, align: align, cells: cells, raw: match[0] };
  1456. for (let i = 0; i < token.align.length; i++) {
  1457. if (/^ *-+: *$/.test(token.align[i])) {
  1458. token.align[i] = 'right';
  1459. }
  1460. else if (/^ *:-+: *$/.test(token.align[i])) {
  1461. token.align[i] = 'center';
  1462. }
  1463. else if (/^ *:-+ *$/.test(token.align[i])) {
  1464. token.align[i] = 'left';
  1465. }
  1466. else {
  1467. token.align[i] = null;
  1468. }
  1469. }
  1470. token.cells = token.cells.map((cell) => this._splitCells(cell, token.header.length));
  1471. source = source.substring(token.raw.length);
  1472. tokens.push(token);
  1473. continue;
  1474. }
  1475. }
  1476. match = this._hrRegExp.exec(source);
  1477. if (match) {
  1478. source = source.substring(match[0].length);
  1479. tokens.push({ type: 'hr' });
  1480. continue;
  1481. }
  1482. match = this._blockquoteRegExp.exec(source);
  1483. if (match) {
  1484. source = source.substring(match[0].length);
  1485. const text = match[0].replace(/^ *> ?/gm, '');
  1486. tokens.push({ type: 'blockquote', text: text, tokens: this._tokenize(text, [], links, top) });
  1487. continue;
  1488. }
  1489. match = this._listRegExp.exec(source);
  1490. if (match) {
  1491. let raw = match[0];
  1492. const bull = match[2];
  1493. const ordered = bull.length > 1;
  1494. const parent = bull[bull.length - 1] === ')';
  1495. const list = { type: 'list', raw: raw, ordered: ordered, start: ordered ? +bull.slice(0, -1) : '', loose: false, items: [] };
  1496. const itemMatch = match[0].match(this._itemRegExp);
  1497. let next = false;
  1498. const length = itemMatch.length;
  1499. for (let i = 0; i < length; i++) {
  1500. let item = itemMatch[i];
  1501. raw = item;
  1502. let space = item.length;
  1503. item = item.replace(/^ *([*+-]|\d+[.)]) ?/, '');
  1504. if (~item.indexOf('\n ')) {
  1505. space -= item.length;
  1506. item = item.replace(new RegExp('^ {1,' + space + '}', 'gm'), '');
  1507. }
  1508. if (i !== length - 1) {
  1509. const bullet = this._bulletRegExp.exec(itemMatch[i + 1])[0];
  1510. if (ordered ? bullet.length === 1 || (!parent && bullet[bullet.length - 1] === ')') : (bullet.length > 1)) {
  1511. const addBack = itemMatch.slice(i + 1).join('\n');
  1512. list.raw = list.raw.substring(0, list.raw.length - addBack.length);
  1513. i = length - 1;
  1514. }
  1515. }
  1516. let loose = next || /\n\n(?!\s*$)/.test(item);
  1517. if (i !== length - 1) {
  1518. next = item.charAt(item.length - 1) === '\n';
  1519. if (!loose) {
  1520. loose = next;
  1521. }
  1522. }
  1523. if (loose) {
  1524. list.loose = true;
  1525. }
  1526. const task = /^\[[ xX]\] /.test(item);
  1527. let checked = undefined;
  1528. if (task) {
  1529. checked = item[1] !== ' ';
  1530. item = item.replace(/^\[[ xX]\] +/, '');
  1531. }
  1532. list.items.push({ type: 'list_item', raw, task: task, checked: checked, loose: loose, text: item });
  1533. }
  1534. source = source.substring(list.raw.length);
  1535. for (const item of list.items) {
  1536. item.tokens = this._tokenize(item.text, [], links, false);
  1537. }
  1538. tokens.push(list);
  1539. continue;
  1540. }
  1541. match = this._htmlRegExp.exec(source);
  1542. if (match) {
  1543. source = source.substring(match[0].length);
  1544. tokens.push({ type: 'html', pre: (match[1] === 'pre' || match[1] === 'script' || match[1] === 'style'), text: match[0] });
  1545. continue;
  1546. }
  1547. if (top) {
  1548. match = this._defRegExp.exec(source);
  1549. if (match) {
  1550. source = source.substring(match[0].length);
  1551. match[3] = match[3] ? match[3].substring(1, match[3].length - 1) : match[3];
  1552. const tag = match[1].toLowerCase().replace(/\s+/g, ' ');
  1553. if (!links.has(tag)) {
  1554. links.set(tag, { href: match[2], title: match[3] });
  1555. }
  1556. continue;
  1557. }
  1558. }
  1559. match = this._tableRegExp.exec(source);
  1560. if (match) {
  1561. const header = this._splitCells(match[1].replace(/^ *| *\| *$/g, ''));
  1562. const align = match[2].replace(/^ *|\| *$/g, '').split(/ *\| */);
  1563. if (header.length === align.length) {
  1564. const cells = match[3] ? match[3].replace(/\n$/, '').split('\n') : [];
  1565. const token = { type: 'table', header: header, align: align, cells: cells, raw: match[0] };
  1566. for (let i = 0; i < token.align.length; i++) {
  1567. if (/^ *-+: *$/.test(token.align[i])) {
  1568. token.align[i] = 'right';
  1569. }
  1570. else if (/^ *:-+: *$/.test(token.align[i])) {
  1571. token.align[i] = 'center';
  1572. }
  1573. else if (/^ *:-+ *$/.test(token.align[i])) {
  1574. token.align[i] = 'left';
  1575. }
  1576. else {
  1577. token.align[i] = null;
  1578. }
  1579. }
  1580. token.cells = token.cells.map((cell) => this._splitCells(cell.replace(/^ *\| *| *\| *$/g, ''), token.header.length));
  1581. source = source.substring(token.raw.length);
  1582. tokens.push(token);
  1583. continue;
  1584. }
  1585. }
  1586. match = this._lheadingRegExp.exec(source);
  1587. if (match) {
  1588. source = source.substring(match[0].length);
  1589. tokens.push({ type: 'heading', depth: match[2].charAt(0) === '=' ? 1 : 2, text: match[1] });
  1590. continue;
  1591. }
  1592. if (top) {
  1593. match = this._paragraphRegExp.exec(source);
  1594. if (match) {
  1595. source = source.substring(match[0].length);
  1596. tokens.push({ type: 'paragraph', text: match[1].charAt(match[1].length - 1) === '\n' ? match[1].slice(0, -1) : match[1] });
  1597. continue;
  1598. }
  1599. }
  1600. match = this._textRegExp.exec(source);
  1601. if (match) {
  1602. source = source.substring(match[0].length);
  1603. const lastToken = tokens[tokens.length - 1];
  1604. if (lastToken && lastToken.type === 'text') {
  1605. lastToken.text += '\n' + match[0];
  1606. }
  1607. else {
  1608. tokens.push({ type: 'text', text: match[0] });
  1609. }
  1610. continue;
  1611. }
  1612. throw new Error("Unexpected '" + source.charCodeAt(0) + "'.");
  1613. }
  1614. return tokens;
  1615. }
  1616. _tokenizeInline(source, links, inLink, inRawBlock, prevChar) {
  1617. const tokens = [];
  1618. let maskedSource = source;
  1619. if (links.size > 0) {
  1620. while (maskedSource) {
  1621. const match = this._reflinkSearchRegExp.exec(maskedSource);
  1622. if (match) {
  1623. if (links.has(match[0].slice(match[0].lastIndexOf('[') + 1, -1))) {
  1624. maskedSource = maskedSource.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSource.slice(this._reflinkSearchRegExp.lastIndex);
  1625. }
  1626. continue;
  1627. }
  1628. break;
  1629. }
  1630. }
  1631. while (maskedSource) {
  1632. const match = this._blockSkipRegExp.exec(maskedSource);
  1633. if (match) {
  1634. maskedSource = maskedSource.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSource.slice(this._blockSkipRegExp.lastIndex);
  1635. continue;
  1636. }
  1637. break;
  1638. }
  1639. while (source) {
  1640. let match = this._escapeRegExp.exec(source);
  1641. if (match) {
  1642. source = source.substring(match[0].length);
  1643. tokens.push({ type: 'escape', text: this._escape(match[1]) });
  1644. continue;
  1645. }
  1646. match = this._tagRegExp.exec(source);
  1647. if (match) {
  1648. source = source.substring(match[0].length);
  1649. if (!inLink && /^<a /i.test(match[0])) {
  1650. inLink = true;
  1651. }
  1652. else if (inLink && /^<\/a>/i.test(match[0])) {
  1653. inLink = false;
  1654. }
  1655. if (!inRawBlock && /^<(pre|code|kbd|script)(\s|>)/i.test(match[0])) {
  1656. inRawBlock = true;
  1657. }
  1658. else if (inRawBlock && /^<\/(pre|code|kbd|script)(\s|>)/i.test(match[0])) {
  1659. inRawBlock = false;
  1660. }
  1661. tokens.push({ type: 'html', raw: match[0], text: match[0] });
  1662. continue;
  1663. }
  1664. match = this._linkRegExp.exec(source);
  1665. if (match) {
  1666. let index = -1;
  1667. const ref = match[2];
  1668. if (ref.indexOf(')') !== -1) {
  1669. let level = 0;
  1670. for (let i = 0; i < ref.length; i++) {
  1671. switch (ref[i]) {
  1672. case '\\':
  1673. i++;
  1674. break;
  1675. case '(':
  1676. level++;
  1677. break;
  1678. case ')':
  1679. level--;
  1680. if (level < 0) {
  1681. index = i;
  1682. i = ref.length;
  1683. }
  1684. break;
  1685. default:
  1686. break;
  1687. }
  1688. }
  1689. }
  1690. if (index > -1) {
  1691. const length = (match[0].indexOf('!') === 0 ? 5 : 4) + match[1].length + index;
  1692. match[2] = match[2].substring(0, index);
  1693. match[0] = match[0].substring(0, length).trim();
  1694. match[3] = '';
  1695. }
  1696. const title = (match[3] ? match[3].slice(1, -1) : '').replace(this._escapesRegExp, '$1');
  1697. const href = match[2].trim().replace(/^<([\s\S]*)>$/, '$1').replace(this._escapesRegExp, '$1');
  1698. const token = this._outputLink(match, href, title);
  1699. source = source.substring(match[0].length);
  1700. if (token.type === 'link') {
  1701. token.tokens = this._tokenizeInline(token.text, links, true, inRawBlock, '');
  1702. }
  1703. tokens.push(token);
  1704. continue;
  1705. }
  1706. match = this._reflinkRegExp.exec(source) || this._nolinkRegExp.exec(source);
  1707. if (match) {
  1708. let link = (match[2] || match[1]).replace(/\s+/g, ' ');
  1709. link = links.get(link.toLowerCase());
  1710. if (!link || !link.href) {
  1711. const text = match[0].charAt(0);
  1712. source = source.substring(text.length);
  1713. tokens.push({ type: 'text', text: text });
  1714. }
  1715. else {
  1716. source = source.substring(match[0].length);
  1717. const token = this._outputLink(match, link);
  1718. if (token.type === 'link') {
  1719. token.tokens = this._tokenizeInline(token.text, links, true, inRawBlock, '');
  1720. }
  1721. tokens.push(token);
  1722. }
  1723. continue;
  1724. }
  1725. match = this._strongStartRegExp.exec(source);
  1726. if (match && (!match[1] || (match[1] && (prevChar === '' || this._punctuationRegExp.exec(prevChar))))) {
  1727. const masked = maskedSource.slice(-1 * source.length);
  1728. const endReg = match[0] === '**' ? this._strongEndAstRegExp : this._strongEndUndRegExp;
  1729. endReg.lastIndex = 0;
  1730. let cap;
  1731. while ((match = endReg.exec(masked)) != null) {
  1732. cap = this._strongMiddleRegExp.exec(masked.slice(0, match.index + 3));
  1733. if (cap) {
  1734. break;
  1735. }
  1736. }
  1737. if (cap) {
  1738. const text = source.substring(2, cap[0].length - 2);
  1739. source = source.substring(cap[0].length);
  1740. tokens.push({ type: 'strong', text: text, tokens: this._tokenizeInline(text, links, inLink, inRawBlock, '') });
  1741. continue;
  1742. }
  1743. }
  1744. match = this._emStartRegExp.exec(source);
  1745. if (match && (!match[1] || (match[1] && (prevChar === '' || this._punctuationRegExp.exec(prevChar))))) {
  1746. const masked = maskedSource.slice(-1 * source.length);
  1747. const endReg = match[0] === '*' ? this._emEndAstRegExp : this._emEndUndRegExp;
  1748. endReg.lastIndex = 0;
  1749. let cap;
  1750. while ((match = endReg.exec(masked)) != null) {
  1751. cap = this._emMiddleRegExp.exec(masked.slice(0, match.index + 2));
  1752. if (cap) {
  1753. break;
  1754. }
  1755. }
  1756. if (cap) {
  1757. const text = source.slice(1, cap[0].length - 1);
  1758. source = source.substring(cap[0].length);
  1759. tokens.push({ type: 'em', text: text, tokens: this._tokenizeInline(text, links, inLink, inRawBlock, '') });
  1760. continue;
  1761. }
  1762. }
  1763. match = this._codespanRegExp.exec(source);
  1764. if (match) {
  1765. source = source.substring(match[0].length);
  1766. let content = match[2].replace(/\n/g, ' ');
  1767. if (/[^ ]/.test(content) && content.startsWith(' ') && content.endsWith(' ')) {
  1768. content = content.substring(1, content.length - 1);
  1769. }
  1770. tokens.push({ type: 'codespan', text: this._encode(content) });
  1771. continue;
  1772. }
  1773. match = this._brRegExp.exec(source);
  1774. if (match) {
  1775. source = source.substring(match[0].length);
  1776. tokens.push({ type: 'br' });
  1777. continue;
  1778. }
  1779. match = this._delRegExp.exec(source);
  1780. if (match) {
  1781. source = source.substring(match[0].length);
  1782. const text = match[1];
  1783. tokens.push({ type: 'del', text: text, tokens: this._tokenizeInline(text, links, inLink, inRawBlock, '') });
  1784. continue;
  1785. }
  1786. match = this._autolinkRegExp.exec(source);
  1787. if (match) {
  1788. source = source.substring(match[0].length);
  1789. const text = this._escape(match[1]);
  1790. const href = match[2] === '@' ? 'mailto:' + text : text;
  1791. tokens.push({ type: 'link', text: text, href: href, tokens: [ { type: 'text', raw: text, text } ] });
  1792. continue;
  1793. }
  1794. if (!inLink) {
  1795. match = this._urlRegExp.exec(source);
  1796. if (match) {
  1797. const email = match[2] === '@';
  1798. if (!email) {
  1799. let prevCapZero;
  1800. do {
  1801. prevCapZero = match[0];
  1802. match[0] = this._backpedalRegExp.exec(match[0])[0];
  1803. } while (prevCapZero !== match[0]);
  1804. }
  1805. const text = this._escape(match[0]);
  1806. const href = email ? ('mailto:' + text) : (match[1] === 'www.' ? 'http://' + text : text);
  1807. source = source.substring(match[0].length);
  1808. tokens.push({ type: 'link', text: text, href: href, tokens: [ { type: 'text', text: text } ] });
  1809. continue;
  1810. }
  1811. }
  1812. match = this._textspanRegExp.exec(source);
  1813. if (match) {
  1814. source = source.substring(match[0].length);
  1815. prevChar = match[0].slice(-1);
  1816. tokens.push({ type: 'text' , text: inRawBlock ? match[0] : this._escape(match[0]) });
  1817. continue;
  1818. }
  1819. throw new Error("Unexpected '" + source.charCodeAt(0) + "'.");
  1820. }
  1821. return tokens;
  1822. }
  1823. _tokenizeBlock(tokens, links) {
  1824. for (const token of tokens) {
  1825. switch (token.type) {
  1826. case 'paragraph':
  1827. case 'text':
  1828. case 'heading': {
  1829. token.tokens = this._tokenizeInline(token.text, links, false, false, '');
  1830. break;
  1831. }
  1832. case 'table': {
  1833. token.tokens = {};
  1834. token.tokens.header = token.header.map((header) => this._tokenizeInline(header, links, false, false, ''));
  1835. token.tokens.cells = token.cells.map((cell) => cell.map((row) => this._tokenizeInline(row, links, false, false, '')));
  1836. break;
  1837. }
  1838. case 'blockquote': {
  1839. this._tokenizeBlock(token.tokens, links);
  1840. break;
  1841. }
  1842. case 'list': {
  1843. for (const item of token.items) {
  1844. this._tokenizeBlock(item.tokens, links);
  1845. }
  1846. break;
  1847. }
  1848. default: {
  1849. break;
  1850. }
  1851. }
  1852. }
  1853. }
  1854. _render(tokens, slugs, top) {
  1855. let html = '';
  1856. while (tokens.length > 0) {
  1857. const token = tokens.shift();
  1858. switch (token.type) {
  1859. case 'space': {
  1860. continue;
  1861. }
  1862. case 'hr': {
  1863. html += '<hr>\n';
  1864. continue;
  1865. }
  1866. case 'heading': {
  1867. const level = token.depth;
  1868. const id = this._slug(slugs, this._renderInline(token.tokens, true));
  1869. html += '<h' + level + ' id="' + id + '">' + this._renderInline(token.tokens) + '</h' + level + '>\n';
  1870. continue;
  1871. }
  1872. case 'code': {
  1873. const code = token.text;
  1874. const language = (token.language || '').match(/\S*/)[0];
  1875. html += '<pre><code' + (language ? ' class="' + 'language-' + this._encode(language) + '"' : '') + '>' + (token.escaped ? code : this._encode(code)) + '</code></pre>\n';
  1876. continue;
  1877. }
  1878. case 'table': {
  1879. let header = '';
  1880. let cell = '';
  1881. for (let j = 0; j < token.header.length; j++) {
  1882. const content = this._renderInline(token.tokens.header[j]);
  1883. const align = token.align[j];
  1884. cell += '<th' + (align ? ' align="' + align + '"' : '') + '>' + content + '</th>\n';
  1885. }
  1886. header += '<tr>\n' + cell + '</tr>\n';
  1887. let body = '';
  1888. for (let j = 0; j < token.cells.length; j++) {
  1889. const row = token.tokens.cells[j];
  1890. cell = '';
  1891. for (let k = 0; k < row.length; k++) {
  1892. const content = this._renderInline(row[k]);
  1893. const align = token.align[k];
  1894. cell += '<td' + (align ? ' align="' + align + '"' : '') + '>' + content + '</td>\n';
  1895. }
  1896. body += '<tr>\n' + cell + '</tr>\n';
  1897. }
  1898. html += '<table>\n<thead>\n' + header + '</thead>\n' + (body ? '<tbody>' + body + '</tbody>' : body) + '</table>\n';
  1899. continue;
  1900. }
  1901. case 'blockquote': {
  1902. html += '<blockquote>\n' + this._render(token.tokens, slugs, true) + '</blockquote>\n';
  1903. continue;
  1904. }
  1905. case 'list': {
  1906. const ordered = token.ordered;
  1907. const start = token.start;
  1908. const loose = token.loose;
  1909. let body = '';
  1910. for (const item of token.items) {
  1911. let itemBody = '';
  1912. if (item.task) {
  1913. const checkbox = '<input ' + (item.checked ? 'checked="" ' : '') + 'disabled="" type="checkbox"' + '> ';
  1914. if (loose) {
  1915. if (item.tokens.length > 0 && item.tokens[0].type === 'text') {
  1916. item.tokens[0].text = checkbox + ' ' + item.tokens[0].text;
  1917. if (item.tokens[0].tokens && item.tokens[0].tokens.length > 0 && item.tokens[0].tokens[0].type === 'text') {
  1918. item.tokens[0].tokens[0].text = checkbox + ' ' + item.tokens[0].tokens[0].text;
  1919. }
  1920. }
  1921. else {
  1922. item.tokens.unshift({ type: 'text', text: checkbox });
  1923. }
  1924. }
  1925. else {
  1926. itemBody += checkbox;
  1927. }
  1928. }
  1929. itemBody += this._render(item.tokens, slugs, loose);
  1930. body += '<li>' + itemBody + '</li>\n';
  1931. }
  1932. const type = (ordered ? 'ol' : 'ul');
  1933. html += '<' + type + (ordered && start !== 1 ? (' start="' + start + '"') : '') + '>\n' + body + '</' + type + '>\n';
  1934. continue;
  1935. }
  1936. case 'html': {
  1937. html += token.text;
  1938. continue;
  1939. }
  1940. case 'paragraph': {
  1941. html += '<p>' + this._renderInline(token.tokens) + '</p>\n';
  1942. continue;
  1943. }
  1944. case 'text': {
  1945. html += top ? '<p>' : '';
  1946. html += token.tokens ? this._renderInline(token.tokens) : token.text;
  1947. while (tokens.length > 0 && tokens[0].type === 'text') {
  1948. const token = tokens.shift();
  1949. html += '\n' + (token.tokens ? this._renderInline(token.tokens) : token.text);
  1950. }
  1951. html += top ? '</p>\n' : '';
  1952. continue;
  1953. }
  1954. default: {
  1955. throw new Error("Unexpected token type '" + token.type + "'.");
  1956. }
  1957. }
  1958. }
  1959. return html;
  1960. }
  1961. _renderInline(tokens, slug) {
  1962. let html = '';
  1963. for (const token of tokens) {
  1964. switch (token.type) {
  1965. case 'escape':
  1966. case 'html':
  1967. case 'text': {
  1968. html += token.text;
  1969. break;
  1970. }
  1971. case 'link': {
  1972. const text = this._renderInline(token.tokens, slug);
  1973. html += slug ? text : '<a href="' + token.href + '"' + (token.title ? ' title="' + token.title + '"' : '') + ' target="_blank">' + text + '</a>';
  1974. break;
  1975. }
  1976. case 'image': {
  1977. html += slug ? token.text : '<img src="' + token.href + '" alt="' + token.text + '"' + (token.title ? ' title="' + token.title + '"' : '') + '>';
  1978. break;
  1979. }
  1980. case 'strong': {
  1981. const text = this._renderInline(token.tokens, slug);
  1982. html += slug ? text : '<strong>' + text + '</strong>';
  1983. break;
  1984. }
  1985. case 'em': {
  1986. const text = this._renderInline(token.tokens, slug);
  1987. html += slug ? text : '<em>' + text + '</em>';
  1988. break;
  1989. }
  1990. case 'codespan': {
  1991. html += slug ? token.text : '<code>' + token.text + '</code>';
  1992. break;
  1993. }
  1994. case 'br': {
  1995. html += slug ? '' : '<br>';
  1996. break;
  1997. }
  1998. case 'del': {
  1999. const text = this._renderInline(token.tokens, slug);
  2000. html += slug ? text : '<del>' + text + '</del>';
  2001. break;
  2002. }
  2003. default: {
  2004. throw new Error("Unexpected token type '" + token.type + "'.");
  2005. }
  2006. }
  2007. }
  2008. return html;
  2009. }
  2010. _outputLink(match, href, title) {
  2011. title = title ? this._escape(title) : null;
  2012. const text = match[1].replace(/\\([[\]])/g, '$1');
  2013. return match[0].charAt(0) !== '!' ?
  2014. { type: 'link', href: href, title: title, text: text } :
  2015. { type: 'image', href: href, title: title, text: this._escape(text) };
  2016. }
  2017. _splitCells(tableRow, count) {
  2018. const row = tableRow.replace(/\|/g, (match, offset, str) => {
  2019. let escaped = false;
  2020. let position = offset;
  2021. while (--position >= 0 && str[position] === '\\') {
  2022. escaped = !escaped;
  2023. }
  2024. return escaped ? '|' : ' |';
  2025. });
  2026. const cells = row.split(/ \|/);
  2027. if (cells.length > count) {
  2028. cells.splice(count);
  2029. }
  2030. else {
  2031. while (cells.length < count) {
  2032. cells.push('');
  2033. }
  2034. }
  2035. return cells.map((cell) => cell.trim().replace(/\\\|/g, '|'));
  2036. }
  2037. _slug(slugs, value) {
  2038. value = value.replace(/&(#(?:\d+)|(?:#x[0-9A-Fa-f]+)|(?:\w+));?/ig, (_, n) => {
  2039. n = n.toLowerCase();
  2040. if (n === 'colon') {
  2041. return ':';
  2042. }
  2043. if (n.charAt(0) === '#') {
  2044. return String.fromCharCode(n.charAt(1) === 'x' ? parseInt(n.substring(2), 16) : +n.substring(1));
  2045. }
  2046. return '';
  2047. });
  2048. value = value.toLowerCase().trim()
  2049. .replace(/<[!/a-z].*?>/ig, '')
  2050. .replace(/[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,./:;<=>?@[\]^`{|}~]/g, '')
  2051. .replace(/\s/g, '-');
  2052. let slug = value;
  2053. let count = 0;
  2054. if (slugs.has(value)) {
  2055. count = slugs.get(value);
  2056. do {
  2057. count++;
  2058. slug = value + '-' + count;
  2059. }
  2060. while (slugs.has(slug));
  2061. }
  2062. slugs.set(value, count);
  2063. slugs.set(slug, 0);
  2064. return slug;
  2065. }
  2066. _encode(content) {
  2067. if (this._escapeTestRegExp.test(content)) {
  2068. return content.replace(this._escapeReplaceRegExp, (ch) => this._escapeReplacementsMap[ch]);
  2069. }
  2070. return content;
  2071. }
  2072. _escape(content) {
  2073. if (this._escapeTestNoEncodeRegExp.test(content)) {
  2074. return content.replace(this._escapeReplaceNoEncodeRegExp, (ch) => this._escapeReplacementsMap[ch]);
  2075. }
  2076. return content;
  2077. }
  2078. };
  2079. if (typeof module !== 'undefined' && typeof module.exports === 'object') {
  2080. module.exports.Sidebar = sidebar.Sidebar;
  2081. module.exports.ModelSidebar = sidebar.ModelSidebar;
  2082. module.exports.NodeSidebar = sidebar.NodeSidebar;
  2083. module.exports.DocumentationSidebar = sidebar.DocumentationSidebar;
  2084. module.exports.FindSidebar = sidebar.FindSidebar;
  2085. module.exports.Formatter = sidebar.Formatter;
  2086. }