ozdenui 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (149) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +14 -0
  3. data/NOTICE +5 -0
  4. data/README.md +242 -0
  5. data/exe/ozdenui +17 -0
  6. data/lib/ozdenui/AGENTS.md +210 -0
  7. data/lib/ozdenui/COMPONENTS.md +3017 -0
  8. data/lib/ozdenui/app.rb +749 -0
  9. data/lib/ozdenui/command.rb +107 -0
  10. data/lib/ozdenui/components.rb +368 -0
  11. data/lib/ozdenui/dialogs.rb +212 -0
  12. data/lib/ozdenui/files.rb +124 -0
  13. data/lib/ozdenui/icon.png +0 -0
  14. data/lib/ozdenui/kit.json +1 -0
  15. data/lib/ozdenui/kit.rb +115 -0
  16. data/lib/ozdenui/look.rb +229 -0
  17. data/lib/ozdenui/objects.rb +652 -0
  18. data/lib/ozdenui/server.rb +270 -0
  19. data/lib/ozdenui/values.rb +273 -0
  20. data/lib/ozdenui/version.rb +15 -0
  21. data/lib/ozdenui/web/basic/LICENSE +14 -0
  22. data/lib/ozdenui/web/basic/basic.min.css +1 -0
  23. data/lib/ozdenui/web/basic/basic.min.js +78 -0
  24. data/lib/ozdenui/web/basic/font/open-sans/LICENSE.txt +206 -0
  25. data/lib/ozdenui/web/basic/font/open-sans/OpenSans-Bold.ttf +0 -0
  26. data/lib/ozdenui/web/basic/font/open-sans/OpenSans-Regular.ttf +0 -0
  27. data/lib/ozdenui/web/basic/scroll-bar.min.js +3 -0
  28. data/lib/ozdenui/web/bridge.js +584 -0
  29. data/lib/ozdenui/web/comp/accordion.js +659 -0
  30. data/lib/ozdenui/web/comp/action-button/add.png +0 -0
  31. data/lib/ozdenui/web/comp/action-button.js +521 -0
  32. data/lib/ozdenui/web/comp/action-tab-bar.js +383 -0
  33. data/lib/ozdenui/web/comp/app-bar.js +237 -0
  34. data/lib/ozdenui/web/comp/badge.js +292 -0
  35. data/lib/ozdenui/web/comp/bottom-bar.js +555 -0
  36. data/lib/ozdenui/web/comp/breadcrumbs.js +524 -0
  37. data/lib/ozdenui/web/comp/canvas.js +652 -0
  38. data/lib/ozdenui/web/comp/chat-view.js +827 -0
  39. data/lib/ozdenui/web/comp/check-box.js +490 -0
  40. data/lib/ozdenui/web/comp/code-editor.js +965 -0
  41. data/lib/ozdenui/web/comp/color-picker.js +636 -0
  42. data/lib/ozdenui/web/comp/context-menu.js +951 -0
  43. data/lib/ozdenui/web/comp/currency-field.js +209 -0
  44. data/lib/ozdenui/web/comp/data-table/arrow.svg +2 -0
  45. data/lib/ozdenui/web/comp/data-table.js +937 -0
  46. data/lib/ozdenui/web/comp/date-picker.js +1055 -0
  47. data/lib/ozdenui/web/comp/dialog.js +992 -0
  48. data/lib/ozdenui/web/comp/email-field.js +65 -0
  49. data/lib/ozdenui/web/comp/file-picker.js +920 -0
  50. data/lib/ozdenui/web/comp/filters.js +807 -0
  51. data/lib/ozdenui/web/comp/form.js +602 -0
  52. data/lib/ozdenui/web/comp/full-screen-select/arrow.svg +2 -0
  53. data/lib/ozdenui/web/comp/full-screen-select/cancel.svg +2 -0
  54. data/lib/ozdenui/web/comp/full-screen-select.js +614 -0
  55. data/lib/ozdenui/web/comp/gauge.js +439 -0
  56. data/lib/ozdenui/web/comp/graph-box.js +3911 -0
  57. data/lib/ozdenui/web/comp/grid-group.js +185 -0
  58. data/lib/ozdenui/web/comp/hold-to-confirm-button/trash-red.png +0 -0
  59. data/lib/ozdenui/web/comp/hold-to-confirm-button/trash.png +0 -0
  60. data/lib/ozdenui/web/comp/hold-to-confirm-button.js +289 -0
  61. data/lib/ozdenui/web/comp/image-view.js +598 -0
  62. data/lib/ozdenui/web/comp/item-list.js +1740 -0
  63. data/lib/ozdenui/web/comp/json-editor.js +3221 -0
  64. data/lib/ozdenui/web/comp/json-view.js +597 -0
  65. data/lib/ozdenui/web/comp/label-editor.js +1140 -0
  66. data/lib/ozdenui/web/comp/left-menu/data-table.png +0 -0
  67. data/lib/ozdenui/web/comp/left-menu/home.png +0 -0
  68. data/lib/ozdenui/web/comp/left-menu/reports.png +0 -0
  69. data/lib/ozdenui/web/comp/left-menu/setting.png +0 -0
  70. data/lib/ozdenui/web/comp/left-menu.js +548 -0
  71. data/lib/ozdenui/web/comp/line-progress-bar.js +343 -0
  72. data/lib/ozdenui/web/comp/loading-screen.js +413 -0
  73. data/lib/ozdenui/web/comp/log-view.js +530 -0
  74. data/lib/ozdenui/web/comp/login-page.js +685 -0
  75. data/lib/ozdenui/web/comp/markdown-view.js +1045 -0
  76. data/lib/ozdenui/web/comp/menu-bar.js +624 -0
  77. data/lib/ozdenui/web/comp/mini-graph-box.js +492 -0
  78. data/lib/ozdenui/web/comp/minimal-accent.js +208 -0
  79. data/lib/ozdenui/web/comp/notice.js +229 -0
  80. data/lib/ozdenui/web/comp/number-field.js +128 -0
  81. data/lib/ozdenui/web/comp/numeric-keyboard.js +298 -0
  82. data/lib/ozdenui/web/comp/page-control.js +762 -0
  83. data/lib/ozdenui/web/comp/page-header.js +191 -0
  84. data/lib/ozdenui/web/comp/password-field/hide-btn.png +0 -0
  85. data/lib/ozdenui/web/comp/password-field/hide-btn2.png +0 -0
  86. data/lib/ozdenui/web/comp/password-field/show-btn.png +0 -0
  87. data/lib/ozdenui/web/comp/password-field/show-btn2.png +0 -0
  88. data/lib/ozdenui/web/comp/password-field.js +192 -0
  89. data/lib/ozdenui/web/comp/period-bar.js +436 -0
  90. data/lib/ozdenui/web/comp/phone-field.js +322 -0
  91. data/lib/ozdenui/web/comp/progress-bar.js +992 -0
  92. data/lib/ozdenui/web/comp/property-panel.js +3639 -0
  93. data/lib/ozdenui/web/comp/radio-button.js +527 -0
  94. data/lib/ozdenui/web/comp/range-slider.js +569 -0
  95. data/lib/ozdenui/web/comp/rich-text-editor.js +1064 -0
  96. data/lib/ozdenui/web/comp/scroll-area.js +308 -0
  97. data/lib/ozdenui/web/comp/scrolling-text.js +297 -0
  98. data/lib/ozdenui/web/comp/search-input/clear-round.svg +1 -0
  99. data/lib/ozdenui/web/comp/search-input/clear.svg +1 -0
  100. data/lib/ozdenui/web/comp/search-input/filter.png +0 -0
  101. data/lib/ozdenui/web/comp/search-input/filter.svg +1 -0
  102. data/lib/ozdenui/web/comp/search-input/search.svg +1 -0
  103. data/lib/ozdenui/web/comp/search-input.js +725 -0
  104. data/lib/ozdenui/web/comp/search-results.js +596 -0
  105. data/lib/ozdenui/web/comp/select-box.js +1862 -0
  106. data/lib/ozdenui/web/comp/settings-row.js +299 -0
  107. data/lib/ozdenui/web/comp/side-panel.js +616 -0
  108. data/lib/ozdenui/web/comp/slider-field.js +952 -0
  109. data/lib/ozdenui/web/comp/slider.js +838 -0
  110. data/lib/ozdenui/web/comp/smart-button/alert-black.png +0 -0
  111. data/lib/ozdenui/web/comp/smart-button/clock.png +0 -0
  112. data/lib/ozdenui/web/comp/smart-button.js +981 -0
  113. data/lib/ozdenui/web/comp/smart-table/clock.png +0 -0
  114. data/lib/ozdenui/web/comp/smart-table/down.png +0 -0
  115. data/lib/ozdenui/web/comp/smart-table/scroll.png +0 -0
  116. data/lib/ozdenui/web/comp/smart-table/sort.png +0 -0
  117. data/lib/ozdenui/web/comp/smart-table/up.png +0 -0
  118. data/lib/ozdenui/web/comp/smart-table.js +1883 -0
  119. data/lib/ozdenui/web/comp/sortable-list.js +1181 -0
  120. data/lib/ozdenui/web/comp/spark-line-box.js +738 -0
  121. data/lib/ozdenui/web/comp/splitter.js +411 -0
  122. data/lib/ozdenui/web/comp/status-bar.js +408 -0
  123. data/lib/ozdenui/web/comp/step-bar.js +345 -0
  124. data/lib/ozdenui/web/comp/stepper.js +753 -0
  125. data/lib/ozdenui/web/comp/tabs.js +1366 -0
  126. data/lib/ozdenui/web/comp/tag-input.js +1107 -0
  127. data/lib/ozdenui/web/comp/tap-effect.js +170 -0
  128. data/lib/ozdenui/web/comp/text-area-field.js +233 -0
  129. data/lib/ozdenui/web/comp/text-field.js +1006 -0
  130. data/lib/ozdenui/web/comp/time-line.js +398 -0
  131. data/lib/ozdenui/web/comp/time-picker.js +1032 -0
  132. data/lib/ozdenui/web/comp/tiny-select/arrow.svg +1 -0
  133. data/lib/ozdenui/web/comp/tiny-select.js +322 -0
  134. data/lib/ozdenui/web/comp/tiny-table.js +283 -0
  135. data/lib/ozdenui/web/comp/toast.js +891 -0
  136. data/lib/ozdenui/web/comp/toggle.js +757 -0
  137. data/lib/ozdenui/web/comp/tool-bar.js +760 -0
  138. data/lib/ozdenui/web/comp/tooltip.js +534 -0
  139. data/lib/ozdenui/web/comp/tree-view.js +1179 -0
  140. data/lib/ozdenui/web/comp/ui-effects.js +1097 -0
  141. data/lib/ozdenui/web/comp/ui-standards.js +403 -0
  142. data/lib/ozdenui/web/comp/url-field.js +190 -0
  143. data/lib/ozdenui/web/comp/web-view.js +588 -0
  144. data/lib/ozdenui/web/components.js +123 -0
  145. data/lib/ozdenui/web/index.html +39 -0
  146. data/lib/ozdenui/window.rb +188 -0
  147. data/lib/ozdenui/window_cocoa.rb +221 -0
  148. data/lib/ozdenui.rb +32 -0
  149. metadata +200 -0
@@ -0,0 +1,1045 @@
1
+ /* Bismillah */
2
+
3
+ /*
4
+
5
+ MarkdownView - v26.11
6
+
7
+ UI COMPONENT
8
+ - A text written in Markdown, drawn on the page: a report, the notes of a program, the answer of an AI.
9
+ - Headings, lists (in lists, with numbers, with check marks), quotes, code, tables, lines, links, pictures, **bold**, *italic*,
10
+ ~~deleted~~, `code`.
11
+ - A text is never read as HTML: "<b>" is shown as it is, and a link or a picture with an address like "javascript:..." stays
12
+ a text. No script of a text runs. A picture with a remote address stays a link: a browser loads a picture by itself, so its
13
+ server would learn that the text was opened, and its address can carry data (the answer of an AI). A text you trust:
14
+ remotePictures: 1. A picture of the page and a data: address are always drawn.
15
+ - A text that comes piece by piece: appendText(). Only the blocks that changed are drawn again, and the view follows the end
16
+ until the reader scrolls up.
17
+ - Code: a button copies it. comp/code-editor.js, when it is loaded, gives it colors: "python", "javascript", "go", "json",
18
+ "html", "css", "sql", "bash" (and "py", "js", "sh"...).
19
+ - height: a number or "100%": the text scrolls inside it (basic/scroll-bar.js when it is loaded). "auto": it grows with its text.
20
+ - A click on a link: onLinkClick when it is given, else a new tab. A link to "#a-heading" goes to that heading of the text.
21
+ - Style packages: "light" (default), "dark". Select with styleName (MarkdownView.styles), or later with setTheme().
22
+
23
+ USAGE:
24
+ const view = MarkdownView({
25
+ height: 400,
26
+ markdownText: "# A report\n\nSome **bold** words and a [link](https://example.com).",
27
+ onLinkClick: function (self, address) { println(address); },
28
+ });
29
+ view.setMarkdownText("# Another text");
30
+ view.appendText("\n\nThe next words");
31
+ view.getMarkdownText();
32
+ view.getHeadings(); // [{ id: "another-text", level: 1, text: "Another text" }]
33
+ view.scrollToHeading("another-text");
34
+ view.setTheme("dark");
35
+
36
+ MarkdownView.parse("# Hi").title; // "Hi": the text without a view
37
+
38
+ Started Date: November 2026
39
+ Webpage: https://bug7a.github.io/js-components/
40
+
41
+ Copyright 2020-2026 Bugra Ozden <bugra.ozden@gmail.com> (https://bug7a.github.io/)
42
+ Licensed under the Apache License, Version 2.0
43
+ SPDX-License-Identifier: Apache-2.0
44
+
45
+ */
46
+
47
+ "use strict";
48
+
49
+ // Default values:
50
+ const MarkdownViewDefaults = {
51
+ key: "0", // Your name for this object: read it in a callback (self.key)
52
+ left: 0,
53
+ top: 0,
54
+ width: "100%",
55
+ height: "100%", // A number or "100%": the text scrolls inside it. "auto": it grows with its text.
56
+ markdownText: "", // The text. setMarkdownText(), appendText()
57
+ emptyText: "", // Shown when there is no text
58
+ autoScroll: 1, // 1: with appendText() the view follows the end, until the reader scrolls up
59
+ remotePictures: 0, // 1: a picture with an http address is drawn, not a link (See the note at the top)
60
+ useScrollBar: 1, // basic/scroll-bar.js (when it is loaded), with a height that is not "auto". 0: the scrollbar of the browser
61
+ ariaLabel: "Document",
62
+ copyText: "Copy", // The button of a code
63
+ copiedText: "Copied",
64
+ onLinkClick: null, // function (self, address) {}: a link comes to you, not to the browser. null: it opens in a new tab.
65
+ styleName: "light", // "light", "dark" or a name added to MarkdownView.styles
66
+ style: { // Light style package (default)
67
+ box: { color: "white" },
68
+ page: { padding: [22, 18], gap: 12, maxWidth: 820 }, // WHY maxWidth: A line as wide as a big window is hard to read.
69
+ text: { fontSize: 15, lineRatio: 1.6, textColor: Black(0.8), softColor: Black(0.5), strongColor: Black(0.92) },
70
+ // The sizes and the space above, for the levels 1 to 6. The line under the levels 1 and 2.
71
+ heading: { fontSizes: [27, 22, 18, 16, 15, 14], spaces: [16, 14, 10, 8, 6, 6], lineRatio: 1.3, textColor: Black(0.92), lineColor: Black(0.1), linePadding: 6 },
72
+ link: { color: "#1F7A83", lineColor: "rgba(31, 122, 131, 0.35)" },
73
+ inlineCode: { color: Black(0.06) },
74
+ quote: { textColor: Black(0.62), lineColor: Black(0.16), lineSize: 3, padding: 14 },
75
+ table: { lineColor: Black(0.14), headColor: Black(0.04) },
76
+ line: { color: Black(0.12), space: 6 },
77
+ check: { color: Black(0.3), onColor: "#1F7A83", markColor: "white" },
78
+ code: {
79
+ color: "#F6F6F4",
80
+ border: 1,
81
+ borderColor: Black(0.08),
82
+ round: 8,
83
+ fontSize: 13,
84
+ lineHeight: 20,
85
+ fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace',
86
+ textColor: "#2B2B2A",
87
+ padding: [14, 2],
88
+ bottomPadding: 12,
89
+ bar: { height: 34, paddingRight: 6, fontSize: 12, textColor: Black(0.5) }, // WHY no left: its text stands over the code.
90
+ button: { height: 26, padding: 8, gap: 5, round: 6, fontSize: 12, iconSize: 14, textColor: Black(0.6), hoverColor: Black(0.07) },
91
+ },
92
+ codeStyleName: "light", // The package of CodeEditor whose colors the code takes
93
+ empty: { fontSize: 14, textColor: Black(0.45) },
94
+ focus: { ringColor: "rgba(63, 141, 148, 0.8)" },
95
+ scrollBar: { bar_color: "#9A9A98", bar_mouseOverColor: "#6A6A68", bar_width: 4, bar_round: 3 },
96
+ },
97
+ };
98
+
99
+ const MarkdownView = function (params = {}) {
100
+
101
+ // Merge style package: params.style > MarkdownView.styles[styleName] > MarkdownViewDefaults.style (light)
102
+ const pageStyle = params.style; // WHY: setTheme() puts it over another package.
103
+ basic.applyStylePackage(params, MarkdownView.styles, MarkdownViewDefaults);
104
+
105
+ // Merge params:
106
+ mergeIntoIfMissing(params, MarkdownViewDefaults);
107
+
108
+ // WHY before startObject: box.height never reads "auto".
109
+ const isAutoHeight = (String(params.height) === "auto");
110
+
111
+ // BOX: Component container
112
+ const box = startObject(params);
113
+
114
+ // *** PRIVATE VARIABLES:
115
+ let _s = box.style;
116
+ let entries = []; // { key, object }: the blocks on the view, in order
117
+ const headingLabels = new Map(); // id -> the label of a heading
118
+ let cancelDraw = null;
119
+ let isFollowing = 1; // 1: the reader is at the end, and the view follows it
120
+ let lastScrollTop = 0;
121
+ const DRAW_DELAY = 33; // ms: pieces of a text that come faster are drawn together
122
+ const BOTTOM_SPACE = 30; // px: this near to the end is at the end
123
+ const HEADING_SPACE = 12; // px: above a heading the view goes to
124
+ const COPIED_TIME = 1500; // ms: how long the button says it is copied
125
+
126
+ // *** PUBLIC VARIABLES:
127
+ // NOTE: Default values are also public variables. (box.markdownText, box.emptyText...)
128
+
129
+ // *** PRIVATE FUNCTIONS:
130
+
131
+ // The style of a package, with the style of the page over it. (setTheme)
132
+ const styleOfPackage = function (styleName) {
133
+ const fresh = { styleName: styleName, style: pageStyle };
134
+ basic.applyStylePackage(fresh, MarkdownView.styles, MarkdownViewDefaults);
135
+ mergeIntoIfMissing(fresh, MarkdownViewDefaults);
136
+ return fresh.style;
137
+ };
138
+
139
+ const parseText = function () {
140
+ return MarkdownView.parse(box.markdownText, { remotePictures: box.remotePictures });
141
+ };
142
+
143
+ // BLOCKS: each kind of block as objects
144
+
145
+ const createTextLabel = function (html, look) {
146
+ const label = Label(Object.assign({ width: "100%", text: html, selectable: 1 }, look));
147
+ label.elem.classList.add("markdown-view-text");
148
+ return label;
149
+ };
150
+
151
+ const createParagraph = function (block, index, textColor) {
152
+ return createTextLabel(block.html, {
153
+ fontSize: _s.text.fontSize,
154
+ lineHeight: Math.round(_s.text.fontSize * _s.text.lineRatio),
155
+ textColor: textColor,
156
+ });
157
+ };
158
+
159
+ const createHeading = function (block, index) {
160
+ const look = _s.heading;
161
+ const fontSize = look.fontSizes[block.level - 1];
162
+ const label = createTextLabel(block.html, {
163
+ fontSize: fontSize,
164
+ lineHeight: Math.round(fontSize * look.lineRatio),
165
+ bold: 1,
166
+ textColor: look.textColor,
167
+ role: "heading",
168
+ aria: { level: block.level },
169
+ });
170
+ if (index > 0) label.css.marginTop = look.spaces[block.level - 1] + "px";
171
+ if (block.level <= 2) {
172
+ label.css.borderBottom = "1px solid " + look.lineColor;
173
+ label.css.paddingBottom = look.linePadding + "px";
174
+ }
175
+ headingLabels.set(block.id, label);
176
+ return label;
177
+ };
178
+
179
+ const createQuote = function (block) {
180
+ const look = _s.quote;
181
+ // GROUP: A line on its left, its blocks in it
182
+ const group = VGroup({ width: "100%", height: "auto", align: "left top", gap: _s.page.gap, clipContent: 0 });
183
+ group.css.borderLeft = look.lineSize + "px solid " + look.lineColor;
184
+ group.css.paddingLeft = look.padding + "px";
185
+ group.css.boxSizing = "border-box";
186
+ block.blocks.forEach(function (innerBlock, index) { createBlock(innerBlock, index, look.textColor); });
187
+ endGroup();
188
+ return group;
189
+ };
190
+
191
+ const createLine = function () {
192
+ // BOX: A line across the text
193
+ const line = Box({ width: "100%", height: 1, color: _s.line.color });
194
+ line.css.margin = _s.line.space + "px 0";
195
+ return line;
196
+ };
197
+
198
+ const showCopyState = function (button, isCopied) {
199
+ const text = (isCopied) ? box.copiedText : box.copyText;
200
+ button.text = MarkdownView.copyButtonHtml(text, isCopied);
201
+ button.aria = { label: text };
202
+ };
203
+
204
+ const copyCode = function (button, code) {
205
+ MarkdownView.copyToClipboard(code).then(function () {
206
+ if (button.isRemoved) return;
207
+ showCopyState(button, 1);
208
+ box.after(COPIED_TIME, function () {
209
+ if (!button.isRemoved) showCopyState(button, 0);
210
+ });
211
+ }).catch(function (error) {
212
+ console.warn("MarkdownView: The code could not be copied. " + error);
213
+ });
214
+ };
215
+
216
+ const createCopyButton = function (code) {
217
+ const look = _s.code.button;
218
+ // BUTTON: Copies the code
219
+ const button = Button({
220
+ width: "auto",
221
+ height: look.height,
222
+ color: "transparent",
223
+ round: look.round,
224
+ fontSize: look.fontSize,
225
+ textColor: look.textColor,
226
+ shrink: 0,
227
+ css: { display: "flex", alignItems: "center", gap: look.gap + "px", padding: "0 " + look.padding + "px", whiteSpace: "nowrap" },
228
+ });
229
+ button.minimal = 1;
230
+ button.elem.style.setProperty("--markdown-view-copy-icon", look.iconSize + "px");
231
+ button.setFocusRing(_s.focus.ringColor);
232
+ showCopyState(button, 0);
233
+ button.onHoverChange(function (self, isOver) { self.color = (isOver) ? look.hoverColor : "transparent"; });
234
+ button.on("click", function () { copyCode(button, code); });
235
+ return button;
236
+ };
237
+
238
+ const createCode = function (block) {
239
+ const look = _s.code;
240
+ const card = VGroup({ width: "100%", height: "auto", align: "left top", color: look.color, border: look.border, borderColor: look.borderColor, round: look.round });
241
+ // GROUP: Its language and its button
242
+ const bar = HGroup({ width: "100%", height: look.bar.height, align: "left center", shrink: 0 });
243
+ bar.css.padding = "0 " + look.bar.paddingRight + "px 0 " + look.padding[0] + "px";
244
+ bar.css.boxSizing = "border-box";
245
+ Label({ plainText: block.language, fontSize: look.bar.fontSize, textColor: look.bar.textColor, grow: 1, ellipsis: 1 });
246
+ createCopyButton(block.code);
247
+ endGroup();
248
+ // GROUP: Its lines. WHY scrollX: a line of code is not wrapped.
249
+ const lines = VGroup({ width: "100%", height: "auto", align: "left top", padding: look.padding, clipContent: 0 });
250
+ lines.scrollX = 1;
251
+ lines.css.paddingBottom = look.bottomPadding + "px";
252
+ const label = Label({
253
+ text: MarkdownView.codeHtml(block.code, block.language),
254
+ fontSize: look.fontSize,
255
+ lineHeight: look.lineHeight,
256
+ textColor: look.textColor,
257
+ selectable: 1,
258
+ css: { fontFamily: look.fontFamily, whiteSpace: "pre", width: "max-content", minWidth: "100%", overflow: "visible" },
259
+ });
260
+ label.elem.classList.add("markdown-view-code");
261
+ endGroup();
262
+ endGroup();
263
+ return card;
264
+ };
265
+
266
+ const createBlock = function (block, index, textColor) {
267
+ if (block.type === "heading") return createHeading(block, index);
268
+ if (block.type === "quote") return createQuote(block);
269
+ if (block.type === "code") return createCode(block);
270
+ if (block.type === "line") return createLine();
271
+ return createParagraph(block, index, textColor);
272
+ };
273
+
274
+ // What stayed the same stays on the page, with its selection. WHY: a text that comes piece by piece changes only at its end.
275
+ const drawText = function () {
276
+ cancelDraw = null;
277
+ const blocks = parseText().blocks;
278
+ const keys = blocks.map(function (block) { return JSON.stringify(block); });
279
+ let sameCount = 0;
280
+ while (sameCount < entries.length && entries[sameCount].key === keys[sameCount]) sameCount++;
281
+ entries.splice(sameCount).forEach(function (entry) { entry.object.remove(); });
282
+ createIn(box.blockGroup, function () {
283
+ for (let index = sameCount; index < blocks.length; index++) {
284
+ entries.push({ key: keys[index], object: createBlock(blocks[index], index, _s.text.textColor) });
285
+ }
286
+ });
287
+ paintEmpty();
288
+ };
289
+
290
+ const drawAgain = function () {
291
+ const scrollTop = (box.scrollArea) ? box.scrollArea.elem.scrollTop : 0;
292
+ entries.forEach(function (entry) { entry.object.remove(); });
293
+ entries = [];
294
+ drawText();
295
+ if (box.scrollArea) box.scrollArea.elem.scrollTop = scrollTop;
296
+ };
297
+
298
+ // PAINT: the text of an empty view
299
+ const paintEmpty = function () {
300
+ const isEmpty = (entries.length === 0 && box.emptyText !== "");
301
+ box.lblEmpty.plainText = box.emptyText;
302
+ box.lblEmpty.visible = (isEmpty) ? 1 : 0;
303
+ };
304
+
305
+ const isSelectingText = function () {
306
+ const selection = window.getSelection();
307
+ return !!(selection && !selection.isCollapsed && box.elem.contains(selection.anchorNode));
308
+ };
309
+
310
+ const isAtEnd = function () {
311
+ if (!box.scrollArea) return true;
312
+ const elem = box.scrollArea.elem;
313
+ return (elem.scrollHeight - elem.scrollTop - elem.clientHeight <= BOTTOM_SPACE);
314
+ };
315
+
316
+ const goToEnd = function () {
317
+ const elem = box.scrollArea.elem;
318
+ elem.scrollTop = elem.scrollHeight;
319
+ lastScrollTop = elem.scrollTop; // WHY: Its scroll event comes a frame late, when more text may have come: not a scroll up.
320
+ };
321
+
322
+ const followEnd = function () {
323
+ if (!box.scrollArea || box.autoScroll != 1 || !isFollowing || isSelectingText()) return;
324
+ goToEnd();
325
+ };
326
+
327
+ const drawNewPieces = function () {
328
+ drawText();
329
+ followEnd();
330
+ };
331
+
332
+ // The reader moved: at the end the view follows again, above it the view stays.
333
+ const onScroll = function () {
334
+ const scrollTop = box.scrollArea.elem.scrollTop;
335
+ if (scrollTop === lastScrollTop) return;
336
+ lastScrollTop = scrollTop;
337
+ isFollowing = isAtEnd() ? 1 : 0;
338
+ };
339
+
340
+ const onBlockClick = function (self, event) {
341
+ const link = event.target.closest("a");
342
+ if (!link) return;
343
+ const address = link.getAttribute("href");
344
+ if (address.charAt(0) === "#") {
345
+ event.preventDefault(); // WHY: The address of the page stays: a site can have its own pages after a "#".
346
+ box.scrollToHeading(address.slice(1));
347
+ return;
348
+ }
349
+ if (typeof box.onLinkClick !== "function") return;
350
+ event.preventDefault();
351
+ box.onLinkClick(box, address);
352
+ };
353
+
354
+ const applyScrollBarLook = function () {
355
+ if (!box.scrollBar) return;
356
+ Object.assign(box.scrollBar, _s.scrollBar);
357
+ box.scrollBar.boxScrollBarTop.color = _s.scrollBar.bar_color;
358
+ box.scrollBar.boxScrollBarLeft.color = _s.scrollBar.bar_color;
359
+ };
360
+
361
+ const refreshScroll = function () {
362
+ if (box.scrollBar && box.scrollBar.refreshScroll) box.scrollBar.refreshScroll();
363
+ };
364
+
365
+ // WHY variables: the parts of a text are HTML in a Label, and only a style sheet reaches them.
366
+ const applyTextColors = function () {
367
+ const variables = {
368
+ "--markdown-view-soft": _s.text.softColor,
369
+ "--markdown-view-strong": _s.text.strongColor,
370
+ "--markdown-view-link": _s.link.color,
371
+ "--markdown-view-link-line": _s.link.lineColor,
372
+ "--markdown-view-inline-code": _s.inlineCode.color,
373
+ "--markdown-view-table-line": _s.table.lineColor,
374
+ "--markdown-view-table-head": _s.table.headColor,
375
+ "--markdown-view-check": _s.check.color,
376
+ "--markdown-view-check-on": _s.check.onColor,
377
+ "--markdown-view-check-mark": _s.check.markColor,
378
+ "--markdown-view-ring": _s.focus.ringColor,
379
+ "--markdown-view-mono": _s.code.fontFamily,
380
+ };
381
+ Object.assign(variables, MarkdownView.codeColors(_s.codeStyleName));
382
+ Object.keys(variables).forEach(function (name) { box.elem.style.setProperty(name, variables[name]); });
383
+ };
384
+
385
+ const applyLook = function () {
386
+ box.props(_s.box);
387
+ box.blockGroup.gap = _s.page.gap;
388
+ box.blockGroup.padding = _s.page.padding;
389
+ box.blockGroup.css.maxWidth = _s.page.maxWidth + "px";
390
+ box.lblEmpty.props({ fontSize: _s.empty.fontSize, textColor: _s.empty.textColor });
391
+ if (box.scrollArea) box.scrollArea.setFocusRing(_s.focus.ringColor);
392
+ applyTextColors();
393
+ applyScrollBarLook();
394
+ };
395
+
396
+ // *** PUBLIC FUNCTIONS:
397
+
398
+ // The scroll stays where it is. A new document: scrollToTop() after it.
399
+ box.setMarkdownText = function (text) {
400
+ box.markdownText = (text === null || text === undefined) ? "" : String(text);
401
+ drawText();
402
+ isFollowing = isAtEnd() ? 1 : 0;
403
+ };
404
+ // USAGE: view.setMarkdownText("# A title")
405
+
406
+ // The next piece of the text. WHY later: a stream brings many small pieces, and they are drawn together.
407
+ box.appendText = function (text) {
408
+ if (text === null || text === undefined) return;
409
+ box.markdownText += String(text);
410
+ if (!cancelDraw) cancelDraw = box.after(DRAW_DELAY, drawNewPieces);
411
+ };
412
+ // USAGE: view.appendText(" and the next words")
413
+
414
+ box.getMarkdownText = function () {
415
+ return box.markdownText;
416
+ };
417
+
418
+ box.clear = function () {
419
+ box.setMarkdownText("");
420
+ };
421
+
422
+ box.setEmptyText = function (text) {
423
+ box.emptyText = (text === null || text === undefined) ? "" : String(text);
424
+ paintEmpty();
425
+ };
426
+
427
+ box.setAutoScroll = function (value) {
428
+ box.autoScroll = basic.isOn(value) ? 1 : 0;
429
+ if (box.autoScroll == 1) box.scrollToEnd();
430
+ };
431
+ // USAGE: get: view.autoScroll, set: view.setAutoScroll(0)
432
+
433
+ // [{ id, level, text }]: for a list of the headings next to the text. A heading in a quote too.
434
+ box.getHeadings = function () {
435
+ return MarkdownView.headingsOf(parseText().blocks);
436
+ };
437
+
438
+ // id: as in getHeadings(), or its text as it is written in a link ("#a-heading").
439
+ box.scrollToHeading = function (id) {
440
+ const label = headingLabels.get(MarkdownView.slug(id));
441
+ if (!label || label.isRemoved) return;
442
+ if (!box.scrollArea) {
443
+ label.elem.scrollIntoView({ block: "start" });
444
+ return;
445
+ }
446
+ const area = box.scrollArea.elem;
447
+ const distance = label.elem.getBoundingClientRect().top - area.getBoundingClientRect().top;
448
+ area.scrollTop = Math.max(0, area.scrollTop + distance - HEADING_SPACE);
449
+ };
450
+ // USAGE: view.scrollToHeading("a-heading")
451
+
452
+ box.scrollToTop = function () {
453
+ if (box.scrollArea) box.scrollArea.elem.scrollTop = 0;
454
+ };
455
+
456
+ // It goes to the end, and follows again.
457
+ box.scrollToEnd = function () {
458
+ if (!box.scrollArea) return;
459
+ isFollowing = 1;
460
+ goToEnd();
461
+ };
462
+
463
+ // Another style package while it is on the page. A style given by the page stays over it.
464
+ box.setTheme = function (styleName) {
465
+ box.styleName = styleName;
466
+ _s = styleOfPackage(styleName);
467
+ box.style = _s;
468
+ applyLook();
469
+ drawAgain();
470
+ };
471
+ // USAGE: view.setTheme("dark")
472
+
473
+ // *** OBJECT VIEW:
474
+
475
+ const createBlockGroup = function () {
476
+ // GROUP: The blocks. WHY position: a relative group makes a box of height "auto" wrap it.
477
+ box.blockGroup = VGroup({ width: "100%", height: "auto", align: "left top", position: "relative", clipContent: 0 });
478
+ box.blockGroup.css.margin = "0 auto"; // WHY: In a wide window the text stands in the middle.
479
+ box.blockGroup.css.boxSizing = "border-box";
480
+ box.lblEmpty = Label({ width: "100%", textAlign: "center", visible: 0 });
481
+ endGroup();
482
+ };
483
+
484
+ if (isAutoHeight) {
485
+ createBlockGroup();
486
+ } else {
487
+ // BOX: The place that scrolls
488
+ box.scrollArea = startBox({ left: 0, top: 0, width: "100%", height: "100%", color: "transparent" });
489
+ box.scrollArea.scrollY = 1;
490
+ box.scrollArea.role = "document";
491
+ box.scrollArea.aria = { label: box.ariaLabel };
492
+ box.scrollArea.tabIndex = 0; // WHY: The keys of a focused box scroll it (arrows, Page Up, Home, End).
493
+ createBlockGroup();
494
+ endBox();
495
+ if (box.useScrollBar == 1 && typeof ScrollBar !== "undefined") {
496
+ box.scrollBar = ScrollBar(Object.assign({ scrollableBox: box.scrollArea, neverHide: 0, showDots: 0 }, _s.scrollBar));
497
+ }
498
+ }
499
+
500
+ // *** OBJECT INIT CODE:
501
+
502
+ MarkdownView.addCss();
503
+ applyLook();
504
+ box.listenTo(box.blockGroup.elem, "click", onBlockClick);
505
+ if (box.scrollArea) {
506
+ box.scrollArea.on("scroll", onScroll);
507
+ box.blockGroup.onResize(refreshScroll);
508
+ }
509
+ box.setMarkdownText(box.markdownText);
510
+
511
+ return endObject(box);
512
+
513
+ };
514
+
515
+ // *** STATIC VARIABLES AND FUNCTIONS: THE LOOK OF THE PARTS OF A TEXT
516
+
517
+ MarkdownView.addCss = function () {
518
+ basic.addCss("markdown-view",
519
+ ".markdown-view-text { overflow-wrap: break-word; }" +
520
+ ".markdown-view-text strong { font-family: opensans-bold; font-weight: normal; color: var(--markdown-view-strong); }" +
521
+ '.markdown-view-text[role="heading"] strong { color: inherit; }' +
522
+ ".markdown-view-text em { font-style: italic; }" +
523
+ ".markdown-view-text del { opacity: 0.7; }" +
524
+ ".markdown-view-text code { font-family: var(--markdown-view-mono); font-size: 0.87em; padding: 0.12em 0.36em; border-radius: 4px;" +
525
+ " background: var(--markdown-view-inline-code); }" +
526
+ ".markdown-view-text a { color: var(--markdown-view-link); text-decoration: none; border-bottom: 1px solid var(--markdown-view-link-line); }" +
527
+ ".markdown-view-text a:hover { border-bottom-color: var(--markdown-view-link); }" +
528
+ ".markdown-view-text a:focus-visible { outline: 2px solid var(--markdown-view-ring); outline-offset: 2px; border-radius: 2px; }" +
529
+ ".markdown-view-text img { max-width: 100%; height: auto; border-radius: 6px; vertical-align: top; }" +
530
+ ".markdown-view-text ul, .markdown-view-text ol { margin: 0; padding-left: 24px; }" +
531
+ ".markdown-view-text li { margin: 4px 0; padding-left: 2px; }" +
532
+ ".markdown-view-text li::marker { color: var(--markdown-view-soft); }" +
533
+ ".markdown-view-text li.markdown-view-task { list-style: none; margin-left: -20px; }" +
534
+ ".markdown-view-check { display: inline-block; box-sizing: border-box; width: 15px; height: 15px; margin-right: 8px; vertical-align: -2px;" +
535
+ " border: 1.5px solid var(--markdown-view-check); border-radius: 4px; }" +
536
+ ".markdown-view-check-on { background: var(--markdown-view-check-on); border-color: var(--markdown-view-check-on); position: relative; }" +
537
+ // WHY a turned corner: a check mark with no icon file, in the color of the package.
538
+ ".markdown-view-check-on::after { content: ''; position: absolute; left: 3.5px; top: 0.5px; width: 3px; height: 7px;" +
539
+ " border: solid var(--markdown-view-check-mark); border-width: 0 1.5px 1.5px 0; transform: rotate(45deg); }" +
540
+ ".markdown-view-table { overflow-x: auto; }" +
541
+ ".markdown-view-table table { border-collapse: collapse; font-size: 0.93em; }" +
542
+ ".markdown-view-table th, .markdown-view-table td { border: 1px solid var(--markdown-view-table-line); padding: 7px 10px; text-align: left; vertical-align: top; }" +
543
+ ".markdown-view-table th { font-family: opensans-bold; font-weight: normal; background: var(--markdown-view-table-head); }" +
544
+ ".markdown-view-copy-icon { display: block; flex-shrink: 0; width: var(--markdown-view-copy-icon); height: var(--markdown-view-copy-icon); pointer-events: none; }" +
545
+ ".markdown-view-copy-icon svg { display: block; width: 100%; height: 100%; }" +
546
+ MarkdownView.CODE_KINDS.map(function (kind) {
547
+ return ".markdown-view-code .code-editor-" + kind + " { color: var(--markdown-view-kind-" + kind + "); }";
548
+ }).join(""));
549
+ };
550
+
551
+ MarkdownView.CODE_KINDS = ["keyword", "string", "number", "comment", "builtin", "function", "property", "tag", "attribute", "variable"];
552
+
553
+ // The colors of the code as style variables. WHY from CodeEditor: one list of colors for the code of the kit.
554
+ MarkdownView.codeColors = function (styleName) {
555
+ const variables = {};
556
+ if (typeof CodeEditor === "undefined") return variables;
557
+ const kinds = Object.assign({}, CodeEditor.styles.light.kinds, (CodeEditor.styles[styleName] || {}).kinds);
558
+ MarkdownView.CODE_KINDS.forEach(function (kind) { variables["--markdown-view-kind-" + kind] = kinds[kind]; });
559
+ return variables;
560
+ };
561
+
562
+ MarkdownView.ICONS = {
563
+ copy: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">' +
564
+ '<rect x="9" y="9" width="12" height="12" rx="2"/><path d="M5 15h-.5A1.5 1.5 0 0 1 3 13.5v-9A1.5 1.5 0 0 1 4.5 3h9A1.5 1.5 0 0 1 15 4.5V5"/></svg>',
565
+ check: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round">' +
566
+ '<path d="M5 13l4.5 4.5L19 7"/></svg>',
567
+ };
568
+
569
+ MarkdownView.copyButtonHtml = function (text, isCopied) {
570
+ const icon = (isCopied) ? MarkdownView.ICONS.check : MarkdownView.ICONS.copy;
571
+ return '<span class="markdown-view-copy-icon">' + icon + "</span><span>" + basic.escapeHtml(text) + "</span>";
572
+ };
573
+
574
+ // Returns a Promise.
575
+ MarkdownView.copyToClipboard = function (text) {
576
+ if (navigator.clipboard && window.isSecureContext) return navigator.clipboard.writeText(text);
577
+ // WHY: A page opened from a file (the window of a program) has no navigator.clipboard.
578
+ const area = document.createElement("textarea");
579
+ area.value = text;
580
+ area.style.cssText = "position: fixed; left: -9999px; top: 0;";
581
+ document.body.appendChild(area);
582
+ area.select();
583
+ const isCopied = document.execCommand("copy");
584
+ area.remove();
585
+ return (isCopied) ? Promise.resolve() : Promise.reject(new Error("The browser refused it."));
586
+ };
587
+
588
+ // *** THE COLORS OF THE CODE:
589
+
590
+ MarkdownView.LANGUAGE_NAMES = {
591
+ py: "python", python3: "python",
592
+ js: "javascript", jsx: "javascript", ts: "javascript", tsx: "javascript", typescript: "javascript", node: "javascript",
593
+ sh: "bash", shell: "bash", zsh: "bash", console: "bash",
594
+ golang: "go",
595
+ htm: "html", xml: "html", svg: "html",
596
+ };
597
+ MarkdownView.MAX_COLORED_LETTERS = 200000; // WHY: A longer code stays plain: a stream must not wait for the colors.
598
+
599
+ // A code as HTML: colored when comp/code-editor.js is loaded and knows the language.
600
+ MarkdownView.codeHtml = function (code, language) {
601
+ const name = MarkdownView.LANGUAGE_NAMES[language] || language;
602
+ const definition = (typeof CodeEditor !== "undefined") ? CodeEditor.languages[name] : null;
603
+ if (!definition || code.length > MarkdownView.MAX_COLORED_LETTERS) return basic.escapeHtml(code);
604
+ let state = null;
605
+ return code.split("\n").map(function (line) {
606
+ const result = CodeEditor.tokenizeLine(line, state, definition);
607
+ state = result.state;
608
+ return result.parts.map(CodeEditor.partHtml).join("");
609
+ }).join("\n");
610
+ };
611
+
612
+ // *** THE PARSER: a text -> blocks, with no view. Every text in a block is safe HTML.
613
+ // { type: "heading", level, html, text, id }, { type: "p", html }, { type: "list", html }, { type: "table", html },
614
+ // { type: "quote", blocks }, { type: "code", language, code }, { type: "line" }
615
+
616
+ // options: { remotePictures: 1 }. Returns { blocks, title }.
617
+ MarkdownView.parse = function (text, options = {}) {
618
+ const context = { usedIds: Object.create(null), remotePictures: basic.isOn(options.remotePictures) }; // WHY null: a heading can be named "constructor".
619
+ const lines = String((text === null || text === undefined) ? "" : text).replace(/\r\n?/g, "\n").split("\n");
620
+ const blocks = MarkdownView.parseBlocks(lines, context);
621
+ const firstHeading = MarkdownView.headingsOf(blocks).find(function (heading) { return heading.level === 1; });
622
+ return { blocks: blocks, title: (firstHeading) ? firstHeading.text : "" };
623
+ };
624
+
625
+ MarkdownView.headingsOf = function (blocks) {
626
+ const headings = [];
627
+ blocks.forEach(function (block) {
628
+ if (block.type === "heading") headings.push({ id: block.id, level: block.level, text: block.text });
629
+ if (block.type === "quote") headings.push.apply(headings, MarkdownView.headingsOf(block.blocks));
630
+ });
631
+ return headings;
632
+ };
633
+
634
+ MarkdownView.RE = {
635
+ fence: /^(\s*)(`{3,}|~{3,})\s*([^\s`]*)[^`]*$/,
636
+ heading: /^\s{0,3}(#{1,6})\s+(.*?)\s*#*\s*$/,
637
+ underline: /^\s{0,3}(=+|-{2,})\s*$/,
638
+ line: /^\s{0,3}(?:(?:\*\s*){3,}|(?:-\s*){3,}|(?:_\s*){3,})$/,
639
+ quote: /^\s*>/,
640
+ listItem: /^(\s*)([-*+]|\d+[.)])\s+(.*)$/,
641
+ listNextLine: /^\s{2,}\S/,
642
+ task: /^\[( |x|X)\]\s+(.*)$/,
643
+ tableDivider: /^\s*\|?\s*:?-+:?\s*(\|\s*:?-+:?\s*)*\|?\s*$/,
644
+ hardBreak: / {2,}$|\\$/,
645
+ };
646
+
647
+ MarkdownView.parseBlocks = function (lines, context) {
648
+ const reader = { lines: lines, at: 0, blocks: [], context: context };
649
+ while (reader.at < lines.length) {
650
+ if (lines[reader.at].trim()) {
651
+ MarkdownView.BLOCK_READERS.find(function (read) { return read(reader); });
652
+ } else {
653
+ reader.at++;
654
+ }
655
+ }
656
+ return reader.blocks;
657
+ };
658
+
659
+ MarkdownView.isTableStart = function (lines, at) {
660
+ if (lines[at].indexOf("|") === -1 || at + 1 >= lines.length) return false;
661
+ if (!MarkdownView.RE.tableDivider.test(lines[at + 1])) return false;
662
+ return (MarkdownView.tableCells(lines[at]).length === MarkdownView.tableCells(lines[at + 1]).length);
663
+ };
664
+
665
+ // A line that ends a paragraph.
666
+ MarkdownView.isBlockStart = function (lines, at) {
667
+ const RE = MarkdownView.RE;
668
+ const line = lines[at];
669
+ return (RE.fence.test(line) || RE.heading.test(line) || RE.line.test(line) || RE.quote.test(line) || RE.listItem.test(line) || MarkdownView.isTableStart(lines, at));
670
+ };
671
+
672
+ MarkdownView.uniqueId = function (text, usedIds) {
673
+ const id = MarkdownView.slug(text) || "section";
674
+ usedIds[id] = (usedIds[id] || 0) + 1;
675
+ return (usedIds[id] > 1) ? id + "-" + usedIds[id] : id;
676
+ };
677
+
678
+ MarkdownView.headingBlock = function (level, raw, context) {
679
+ const text = MarkdownView.plainText(raw);
680
+ return { type: "heading", level: level, html: MarkdownView.inlineHtml(raw, context), text: text, id: MarkdownView.uniqueId(text, context.usedIds) };
681
+ };
682
+
683
+ // *** Each reader reads one block at reader.at and returns true, or leaves it and returns false.
684
+
685
+ MarkdownView.readCode = function (reader) {
686
+ const open = MarkdownView.RE.fence.exec(reader.lines[reader.at]);
687
+ if (!open) return false;
688
+ const indentOfLine = new RegExp("^ {0," + open[1].length + "}"); // WHY: A code in a list is written further in.
689
+ const closing = new RegExp("^\\s*" + open[2].charAt(0) + "{" + open[2].length + ",}\\s*$");
690
+ const code = [];
691
+ reader.at++;
692
+ // WHY no error at the end of the text: a code without its last line is a code that is still coming.
693
+ while (reader.at < reader.lines.length && !closing.test(reader.lines[reader.at])) {
694
+ code.push(reader.lines[reader.at].replace(indentOfLine, ""));
695
+ reader.at++;
696
+ }
697
+ reader.at++;
698
+ reader.blocks.push({ type: "code", language: open[3].toLowerCase(), code: code.join("\n") });
699
+ return true;
700
+ };
701
+
702
+ MarkdownView.readHeading = function (reader) {
703
+ const match = MarkdownView.RE.heading.exec(reader.lines[reader.at]);
704
+ if (!match) return false;
705
+ reader.blocks.push(MarkdownView.headingBlock(match[1].length, match[2], reader.context));
706
+ reader.at++;
707
+ return true;
708
+ };
709
+
710
+ // A title, and a line of === or --- under it.
711
+ MarkdownView.readUnderlinedHeading = function (reader) {
712
+ const next = reader.lines[reader.at + 1];
713
+ const match = (next === undefined) ? null : MarkdownView.RE.underline.exec(next);
714
+ if (!match || MarkdownView.isBlockStart(reader.lines, reader.at)) return false;
715
+ const level = (match[1].charAt(0) === "=") ? 1 : 2;
716
+ reader.blocks.push(MarkdownView.headingBlock(level, reader.lines[reader.at].trim(), reader.context));
717
+ reader.at += 2;
718
+ return true;
719
+ };
720
+
721
+ MarkdownView.readLine = function (reader) {
722
+ if (!MarkdownView.RE.line.test(reader.lines[reader.at])) return false;
723
+ reader.blocks.push({ type: "line" });
724
+ reader.at++;
725
+ return true;
726
+ };
727
+
728
+ MarkdownView.readQuote = function (reader) {
729
+ const RE = MarkdownView.RE;
730
+ if (!RE.quote.test(reader.lines[reader.at])) return false;
731
+ const inner = [];
732
+ while (reader.at < reader.lines.length && RE.quote.test(reader.lines[reader.at])) {
733
+ inner.push(reader.lines[reader.at].replace(/^\s*>\s?/, ""));
734
+ reader.at++;
735
+ }
736
+ reader.blocks.push({ type: "quote", blocks: MarkdownView.parseBlocks(inner, reader.context) });
737
+ return true;
738
+ };
739
+
740
+ MarkdownView.isLineOfList = function (line) {
741
+ const RE = MarkdownView.RE;
742
+ if (!line.trim() || RE.fence.test(line) || RE.line.test(line)) return false;
743
+ return (RE.listItem.test(line) || RE.listNextLine.test(line));
744
+ };
745
+
746
+ MarkdownView.readList = function (reader) {
747
+ if (!MarkdownView.RE.listItem.test(reader.lines[reader.at])) return false;
748
+ const lines = [];
749
+ while (reader.at < reader.lines.length && MarkdownView.isLineOfList(reader.lines[reader.at])) {
750
+ lines.push(reader.lines[reader.at]);
751
+ reader.at++;
752
+ }
753
+ reader.blocks.push({ type: "list", html: MarkdownView.listHtml(lines, reader.context) });
754
+ return true;
755
+ };
756
+
757
+ MarkdownView.readTable = function (reader) {
758
+ if (!MarkdownView.isTableStart(reader.lines, reader.at)) return false;
759
+ const head = reader.lines[reader.at];
760
+ const divider = reader.lines[reader.at + 1];
761
+ const rows = [];
762
+ reader.at += 2;
763
+ while (reader.at < reader.lines.length && reader.lines[reader.at].indexOf("|") > -1 && reader.lines[reader.at].trim()) {
764
+ rows.push(reader.lines[reader.at]);
765
+ reader.at++;
766
+ }
767
+ reader.blocks.push({ type: "table", html: MarkdownView.tableHtml(head, divider, rows, reader.context) });
768
+ return true;
769
+ };
770
+
771
+ // WHY last: every other line is a paragraph. It always takes a line, so the parser always goes on.
772
+ MarkdownView.readParagraph = function (reader) {
773
+ const lines = [];
774
+ while (reader.at < reader.lines.length && reader.lines[reader.at].trim()) {
775
+ if (lines.length && MarkdownView.isBlockStart(reader.lines, reader.at)) break;
776
+ lines.push(reader.lines[reader.at]);
777
+ reader.at++;
778
+ }
779
+ const html = lines.map(function (line, index) {
780
+ const hasBreak = (MarkdownView.RE.hardBreak.test(line) && index < lines.length - 1);
781
+ const text = line.trim().replace(/\\$/, "");
782
+ return MarkdownView.inlineHtml(text, reader.context) + ((hasBreak) ? "<br>" : " ");
783
+ }).join("");
784
+ reader.blocks.push({ type: "p", html: html.trim() });
785
+ return true;
786
+ };
787
+
788
+ MarkdownView.BLOCK_READERS = [
789
+ MarkdownView.readCode,
790
+ MarkdownView.readHeading,
791
+ MarkdownView.readUnderlinedHeading,
792
+ MarkdownView.readLine,
793
+ MarkdownView.readQuote,
794
+ MarkdownView.readList,
795
+ MarkdownView.readTable,
796
+ MarkdownView.readParagraph,
797
+ ];
798
+
799
+ // *** A LIST:
800
+
801
+ MarkdownView.readListItems = function (lines) {
802
+ const items = [];
803
+ lines.forEach(function (line) {
804
+ const match = MarkdownView.RE.listItem.exec(line);
805
+ if (match) {
806
+ const isOrdered = /\d/.test(match[2]);
807
+ items.push({ indent: match[1].replace(/\t/g, " ").length, isOrdered: isOrdered, start: parseInt(match[2], 10), text: match[3] });
808
+ } else if (items.length) {
809
+ items[items.length - 1].text += " " + line.trim();
810
+ }
811
+ });
812
+ return items;
813
+ };
814
+
815
+ MarkdownView.listItemHtml = function (item, context) {
816
+ const task = MarkdownView.RE.task.exec(item.text);
817
+ if (!task) return "<li>" + MarkdownView.inlineHtml(item.text, context);
818
+ const checkClass = (task[1] === " ") ? "markdown-view-check" : "markdown-view-check markdown-view-check-on";
819
+ return '<li class="markdown-view-task"><span class="' + checkClass + '"></span>' + MarkdownView.inlineHtml(task[2], context);
820
+ };
821
+
822
+ // WHY start: a text between the items of a list makes two lists, and the numbers must go on.
823
+ MarkdownView.listStartHtml = function (item) {
824
+ if (!item.isOrdered) return "<ul>";
825
+ return (item.start === 1) ? "<ol>" : '<ol start="' + item.start + '">';
826
+ };
827
+
828
+ // By the spaces in front of the items: further in is a list in the item above.
829
+ MarkdownView.listHtml = function (lines, context) {
830
+ const items = MarkdownView.readListItems(lines);
831
+ let position = 0;
832
+ const drawLevel = function (indent) {
833
+ const first = items[position];
834
+ let html = MarkdownView.listStartHtml(first);
835
+ while (position < items.length && items[position].indent === indent) {
836
+ html += MarkdownView.listItemHtml(items[position], context);
837
+ position++;
838
+ if (position < items.length && items[position].indent > indent) html += drawLevel(items[position].indent);
839
+ html += "</li>";
840
+ }
841
+ return html + ((first.isOrdered) ? "</ol>" : "</ul>");
842
+ };
843
+ let html = "";
844
+ while (position < items.length) html += drawLevel(items[position].indent);
845
+ return html;
846
+ };
847
+
848
+ // *** A TABLE:
849
+
850
+ // WHY the mark: a "\|" is a letter of a cell, not its end.
851
+ MarkdownView.tableCells = function (row) {
852
+ const PIPE_MARK = "\u0001";
853
+ return row.trim().replace(/\\\|/g, PIPE_MARK).replace(/^\|/, "").replace(/\|$/, "").split("|").map(function (cell) {
854
+ return cell.split(PIPE_MARK).join("|").trim();
855
+ });
856
+ };
857
+
858
+ // ":--" left, ":--:" center, "--:" right
859
+ MarkdownView.tableAlign = function (dividerCell) {
860
+ const isLeft = dividerCell.startsWith(":");
861
+ const isRight = dividerCell.endsWith(":");
862
+ if (isLeft && isRight) return "center";
863
+ return (isRight) ? "right" : "";
864
+ };
865
+
866
+ MarkdownView.tableRowHtml = function (cells, tag, aligns, context) {
867
+ let html = "<tr>";
868
+ aligns.forEach(function (align, index) {
869
+ const style = (align) ? ' style="text-align: ' + align + '"' : "";
870
+ const text = (index < cells.length) ? cells[index] : "";
871
+ html += "<" + tag + style + ">" + MarkdownView.inlineHtml(text, context) + "</" + tag + ">";
872
+ });
873
+ return html + "</tr>";
874
+ };
875
+
876
+ // WHY the box around it: a wide table scrolls in itself, the text stays where it is.
877
+ MarkdownView.tableHtml = function (head, divider, rows, context) {
878
+ const aligns = MarkdownView.tableCells(divider).map(MarkdownView.tableAlign);
879
+ const bodyHtml = rows.map(function (row) {
880
+ return MarkdownView.tableRowHtml(MarkdownView.tableCells(row), "td", aligns, context);
881
+ }).join("");
882
+ return '<div class="markdown-view-table"><table><thead>' + MarkdownView.tableRowHtml(MarkdownView.tableCells(head), "th", aligns, context) +
883
+ "</thead><tbody>" + bodyHtml + "</tbody></table></div>";
884
+ };
885
+
886
+ // *** IN A LINE:
887
+
888
+ MarkdownView.KEEP_MARK = "\u0000"; // Around the number of a part that is ready
889
+
890
+ MarkdownView.INLINE_RE = {
891
+ code: /(`+)\s?([^`]|[^`].*?[^`])\s?\1(?!`)/g,
892
+ letter: /\\([!-/:-@[-`{-~])/g, // A "\" before any mark of ASCII
893
+ picture: /!\[((?:\\.|[^\]\\])*)\]\(\s*<?((?:[^()\s<>]|\([^()\s]*\))+)>?(?:\s+"[^"]*")?\s*\)/g,
894
+ link: /\[((?:\\.|[^[\]\\]|\[[^\]]*\])*)\]\(\s*<?((?:[^()\s<>]|\([^()\s]*\))+)>?(?:\s+"[^"]*")?\s*\)/g,
895
+ bareAddress: /(^|[\s(*_~])(https?:\/\/[^\s<>]+[^\s<>.,;:)!?'"*_~])/g,
896
+ lineBreak: /&lt;br\s*\/?&gt;/gi,
897
+ strong: /\*\*([^\s*](?:.*?[^\s*])?)\*\*/g,
898
+ strongLine: /(^|[^\w])__([^\s_](?:.*?[^\s_])?)__(?!\w)/g,
899
+ emphasis: /(^|[^*\w])\*([^\s*](?:.*?[^\s*])?)\*(?!\*)/g,
900
+ emphasisLine: /(^|[^\w])_([^\s_](?:.*?[^\s_])?)_(?!\w)/g,
901
+ deleted: /~~([^\s~](?:.*?[^\s~])?)~~/g,
902
+ };
903
+
904
+ // A text of a line as safe HTML. WHY the kept parts: what is inside a code or an address is not read as Markdown again.
905
+ MarkdownView.inlineHtml = function (text, context) {
906
+ const kept = [];
907
+ const keep = function (html) {
908
+ kept.push(html);
909
+ return MarkdownView.KEEP_MARK + (kept.length - 1) + MarkdownView.KEEP_MARK;
910
+ };
911
+ const html = MarkdownView.formatLine(String(text).split(MarkdownView.KEEP_MARK).join(""), keep, context);
912
+ return MarkdownView.putKeptParts(html, kept);
913
+ };
914
+
915
+ MarkdownView.putKeptParts = function (html, kept) {
916
+ const keptPart = new RegExp(MarkdownView.KEEP_MARK + "(\\d+)" + MarkdownView.KEEP_MARK, "g");
917
+ let result = html;
918
+ // WHY again: the label of a link holds kept parts too.
919
+ while (result.indexOf(MarkdownView.KEEP_MARK) > -1) {
920
+ result = result.replace(keptPart, function (all, number) { return kept[Number(number)]; });
921
+ }
922
+ return result;
923
+ };
924
+
925
+ MarkdownView.formatLine = function (text, keep, context) {
926
+ const RE = MarkdownView.INLINE_RE;
927
+ let html = text.replace(RE.code, function (all, marks, code) {
928
+ return keep("<code>" + basic.escapeHtml(code) + "</code>");
929
+ });
930
+ // WHY the addresses before the "\*" letters: a letter that is kept can not be checked, and "a\_b" is an address too.
931
+ html = html.replace(RE.picture, function (all, description, address) {
932
+ return keep(MarkdownView.pictureHtml(MarkdownView.removeEscapes(address), MarkdownView.removeEscapes(description), context));
933
+ });
934
+ html = html.replace(RE.link, function (all, label, address) {
935
+ return keep(MarkdownView.linkHtml(MarkdownView.removeEscapes(address), MarkdownView.formatLine(label, keep, context)));
936
+ });
937
+ html = html.replace(RE.letter, function (all, letter) { return keep(basic.escapeHtml(letter)); });
938
+ html = html.replace(RE.bareAddress, function (all, before, address) {
939
+ return before + keep(MarkdownView.linkHtml(address, basic.escapeHtml(address)));
940
+ });
941
+ return MarkdownView.formatWords(basic.escapeHtml(html));
942
+ };
943
+
944
+ MarkdownView.removeEscapes = function (text) {
945
+ return text.replace(MarkdownView.INLINE_RE.letter, "$1");
946
+ };
947
+
948
+ MarkdownView.formatWords = function (html) {
949
+ const RE = MarkdownView.INLINE_RE;
950
+ return html
951
+ .replace(RE.lineBreak, "<br>")
952
+ .replace(RE.strong, "<strong>$1</strong>")
953
+ .replace(RE.strongLine, "$1<strong>$2</strong>")
954
+ .replace(RE.emphasis, "$1<em>$2</em>")
955
+ .replace(RE.emphasisLine, "$1<em>$2</em>")
956
+ .replace(RE.deleted, "<del>$1</del>");
957
+ };
958
+
959
+ // *** ADDRESSES:
960
+
961
+ MarkdownView.LINK_SCHEMES = /^(https?|mailto|tel):/i;
962
+ MarkdownView.PICTURE_SCHEMES = /^(https?:|data:image\/)/i;
963
+ MarkdownView.REMOTE_ADDRESS = /^(https?:)?\/\//i;
964
+ MarkdownView.ANY_SCHEME = /^[a-z][a-z0-9+.-]*:/i;
965
+
966
+ // WHY the clean text: a browser leaves out these letters in an address, so "java&#9;script:" and "java\tscript:" run as "javascript:".
967
+ MarkdownView.isSafeAddress = function (address, safeSchemes) {
968
+ if (address.indexOf(MarkdownView.KEEP_MARK) > -1) return false; // WHY: A part that comes back later could hide its scheme.
969
+ const clean = address.replace(/[\p{C}\p{Z}]/gu, "");
970
+ return (!MarkdownView.ANY_SCHEME.test(clean) || safeSchemes.test(clean));
971
+ };
972
+
973
+ // A safe address is a link; any other one stays its text.
974
+ MarkdownView.linkHtml = function (address, labelHtml) {
975
+ if (!MarkdownView.isSafeAddress(address, MarkdownView.LINK_SCHEMES)) return labelHtml;
976
+ const newTab = (address.charAt(0) === "#") ? "" : ' target="_blank" rel="noopener noreferrer"';
977
+ return '<a href="' + basic.escapeHtml(address) + '"' + newTab + ">" + labelHtml + "</a>";
978
+ };
979
+
980
+ MarkdownView.pictureHtml = function (address, description, context) {
981
+ const text = basic.escapeHtml(description);
982
+ if (!MarkdownView.isSafeAddress(address, MarkdownView.PICTURE_SCHEMES)) return text;
983
+ if (!context.remotePictures && MarkdownView.REMOTE_ADDRESS.test(address)) return MarkdownView.linkHtml(address, text || basic.escapeHtml(address));
984
+ return '<img src="' + basic.escapeHtml(address) + '" alt="' + text + '">';
985
+ };
986
+
987
+ // *** TEXTS:
988
+
989
+ // A text without the marks of Markdown (a heading in a list of headings).
990
+ MarkdownView.plainText = function (text) {
991
+ return String(text)
992
+ .replace(/!?\[([^\]]*)\]\([^)]*\)/g, "$1")
993
+ .replace(/[`*~]/g, "")
994
+ .replace(/(^|\s)_+|_+(\s|$)/g, "$1$2")
995
+ .replace(/\s+/g, " ")
996
+ .trim();
997
+ };
998
+
999
+ // "Çalışma Özeti" -> "calisma-ozeti". WHY ı apart: it has no accent to take off.
1000
+ MarkdownView.slug = function (text) {
1001
+ return String(text)
1002
+ .replace(/[ıİ]/g, "i")
1003
+ .normalize("NFD")
1004
+ .replace(/\p{M}/gu, "")
1005
+ .toLowerCase()
1006
+ .replace(/[^a-z0-9]+/g, "-")
1007
+ .replace(/^-+|-+$/g, "")
1008
+ .slice(0, 60);
1009
+ };
1010
+
1011
+ // *** STYLE PACKAGES:
1012
+ // USAGE: MarkdownView({ styleName: "dark" })
1013
+ // USAGE: MarkdownView({ style: { text: { fontSize: 16 } } }) // Change only some keys.
1014
+ // NOTE: A new package needs only the keys that differ from the default (light) style.
1015
+ // USAGE: MarkdownView.styles.myStyle = { box: { color: "#FFFDF5" } };
1016
+ MarkdownView.styles = {
1017
+
1018
+ // A white page.
1019
+ light: MarkdownViewDefaults.style,
1020
+
1021
+ // For a dark window; the code takes the dark colors of CodeEditor.
1022
+ dark: {
1023
+ box: { color: "#1C1C1B" },
1024
+ text: { textColor: White(0.8), softColor: White(0.5), strongColor: White(0.94) },
1025
+ heading: { textColor: White(0.94), lineColor: White(0.12) },
1026
+ link: { color: "#6FC6CD", lineColor: "rgba(111, 198, 205, 0.35)" },
1027
+ inlineCode: { color: White(0.1) },
1028
+ quote: { textColor: White(0.62), lineColor: White(0.2) },
1029
+ table: { lineColor: White(0.16), headColor: White(0.06) },
1030
+ line: { color: White(0.14) },
1031
+ check: { color: White(0.35), onColor: "#6FC6CD", markColor: "#1C1C1B" },
1032
+ code: {
1033
+ color: "#232322",
1034
+ borderColor: White(0.08),
1035
+ textColor: "#E6E6E3",
1036
+ bar: { textColor: White(0.5) },
1037
+ button: { textColor: White(0.65), hoverColor: White(0.09) },
1038
+ },
1039
+ codeStyleName: "dark",
1040
+ empty: { textColor: White(0.5) },
1041
+ focus: { ringColor: "rgba(111, 198, 205, 0.8)" },
1042
+ scrollBar: { bar_color: "#8A8A88", bar_mouseOverColor: "#BFBFBD" },
1043
+ },
1044
+
1045
+ };