project_note
Clone
https://git.tahinli.com/Tahinli/project_note.git
0 watching0 forksRust3.6 MiBc49f5e7Tahinli1 month ago2 commits
| 1 | //! Markdown import: POST /import/file creates one note from one file, and GET /import is |
| 2 | //! the page that picks files or a folder and sends them one at a time. |
| 3 | //! |
| 4 | //! The browser sends each path with the selected root folder already stripped. A path |
| 5 | //! with a folder component puts the note in the notebook named by its first segment |
| 6 | //! (found by case-insensitive name, else created). A path without one is unfiled. |
| 7 | |
| 8 | use HashMap; |
| 9 | use Path; |
| 10 | use Arc; |
| 11 | |
| 12 | use Deserialize; |
| 13 | use |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | ; |
| 19 | |
| 20 | use cratesanitize; |
| 21 | use crate; |
| 22 | use crateexport; |
| 23 | use crate; |
| 24 | use crateHub; |
| 25 | use crate; |
| 26 | use crateStore; |
| 27 | |
| 28 | /// Files at least this big are stored as markdown first, then get their doc built |
| 29 | /// (in the background when another import is building one). |
| 30 | const BUILD_LATER_BYTES: usize = 1 << 20; |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | /// POST /import/file: creates a note from one markdown file and answers with its id. |
| 44 | |
| 45 | pub async |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | /// GET /import: picks markdown files or a folder and imports them one by one. |
| 130 | |
| 131 | async |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | /// Collects the chosen files, keeps markdown and text as notes and everything else as |
| 182 | /// media, and posts the notes one at a time so that a notebook created by one file is |
| 183 | /// found by the next. Each note's media references are uploaded first, once per file. |
| 184 | /// A selection of media alone becomes one note per media file. The status line and the |
| 185 | /// done text come from the data attributes on #import, so the script has no strings. |
| 186 | pub const IMPORT_SCRIPT: &str = r#" |
| 187 | (function () { |
| 188 | var root = document.getElementById('import'); |
| 189 | if (!root) return; |
| 190 | var status = document.getElementById('import-status'); |
| 191 | var link = document.getElementById('import-link'); |
| 192 | var files = document.getElementById('import-files'); |
| 193 | var folder = document.getElementById('import-folder'); |
| 194 | var supported = /\.(md|markdown|txt)$/i; |
| 195 | var embedded = /\.(png|jpg|jpeg|gif|webp|avif|bmp|mp3|m4a|wav|flac|aac|ogg|oga|opus|weba|mp4|m4v|webm|mov|ogv)$/i; |
| 196 | var busy = false; |
| 197 | |
| 198 | function fill(template, values) { |
| 199 | return Object.keys(values).reduce(function (text, key) { |
| 200 | return text.split('{' + key + '}').join(String(values[key])); |
| 201 | }, template); |
| 202 | } |
| 203 | |
| 204 | function baseName(path) { |
| 205 | return path.slice(path.lastIndexOf('/') + 1); |
| 206 | } |
| 207 | |
| 208 | function stemOf(name) { |
| 209 | var dot = name.lastIndexOf('.'); |
| 210 | return dot > 0 ? name.slice(0, dot) : name; |
| 211 | } |
| 212 | |
| 213 | // Dotfiles and files inside dot-folders (such as .obsidian) are not imported. |
| 214 | function hidden(path) { |
| 215 | return path.split('/').some(function (part) { return part.charAt(0) === '.'; }); |
| 216 | } |
| 217 | |
| 218 | // Whether the note text names the file, raw or percent-encoded. `lower` is lowercased. |
| 219 | function mentions(lower, name) { |
| 220 | return lower.indexOf(name.toLowerCase()) >= 0 || |
| 221 | lower.indexOf(encodeURI(name).toLowerCase()) >= 0; |
| 222 | } |
| 223 | |
| 224 | // The note text that embeds a media file on its own. |
| 225 | function ownText(item) { |
| 226 | var dest = /\s/.test(item.name) ? '<' + item.name + '>' : item.name; |
| 227 | if (embedded.test(item.name)) return ''; |
| 228 | return '[' + item.name + '](' + dest + ')'; |
| 229 | } |
| 230 | |
| 231 | function upload(item) { |
| 232 | return fetch('/media?name=' + encodeURIComponent(item.file.name), { |
| 233 | method: 'POST', |
| 234 | body: item.file, |
| 235 | credentials: 'same-origin' |
| 236 | }).then(function (res) { |
| 237 | if (!res.ok) throw new Error('upload'); |
| 238 | return res.text(); |
| 239 | }); |
| 240 | } |
| 241 | |
| 242 | function run(notes, media) { |
| 243 | var total = notes.length; |
| 244 | var imported = 0; |
| 245 | var failed = 0; |
| 246 | var index = 0; |
| 247 | var uploads = {}; |
| 248 | busy = true; |
| 249 | link.classList.add('hidden'); |
| 250 | |
| 251 | function finish() { |
| 252 | var text = fill(imported === 1 ? root.dataset.doneOne : root.dataset.doneOther, { n: imported }); |
| 253 | if (failed > 0) text += ' ' + fill(root.dataset.failed, { m: failed }); |
| 254 | status.textContent = text; |
| 255 | link.classList.remove('hidden'); |
| 256 | busy = false; |
| 257 | } |
| 258 | |
| 259 | // Each media file is uploaded once per run. A failure is counted once and its |
| 260 | // reference is left out of every note. |
| 261 | function uploadOnce(item) { |
| 262 | if (!uploads[item.path]) { |
| 263 | uploads[item.path] = upload(item).catch(function () { |
| 264 | failed++; |
| 265 | return ''; |
| 266 | }); |
| 267 | } |
| 268 | return uploads[item.path]; |
| 269 | } |
| 270 | |
| 271 | function next() { |
| 272 | if (index >= total) return finish(); |
| 273 | status.textContent = fill(root.dataset.progress, { done: index + 1, total: total }); |
| 274 | var note = notes[index++]; |
| 275 | note.file.text() |
| 276 | .then(function (text) { |
| 277 | var lower = text.toLowerCase(); |
| 278 | var refs = media.filter(function (item) { return mentions(lower, item.name); }); |
| 279 | var map = {}; |
| 280 | return refs.reduce(function (chain, item) { |
| 281 | return chain.then(function () { |
| 282 | return uploadOnce(item).then(function (url) { |
| 283 | if (url) map[item.path] = url; |
| 284 | }); |
| 285 | }); |
| 286 | }, Promise.resolve()).then(function () { |
| 287 | var body = new URLSearchParams(); |
| 288 | body.set('path', note.path); |
| 289 | body.set('text', text); |
| 290 | body.set('media', JSON.stringify(map)); |
| 291 | return fetch('/import/file', { |
| 292 | method: 'POST', |
| 293 | headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, |
| 294 | body: body, |
| 295 | credentials: 'same-origin' |
| 296 | }); |
| 297 | }); |
| 298 | }) |
| 299 | .then(function (res) { |
| 300 | if (res.ok) imported++; else failed++; |
| 301 | }) |
| 302 | .catch(function () { failed++; }) |
| 303 | .then(next); |
| 304 | } |
| 305 | |
| 306 | next(); |
| 307 | } |
| 308 | |
| 309 | function onChange(input, fromFolder) { |
| 310 | input.addEventListener('change', function () { |
| 311 | if (busy) return; |
| 312 | var picked = Array.prototype.slice.call(input.files || []); |
| 313 | input.value = ''; |
| 314 | var notes = []; |
| 315 | var media = []; |
| 316 | picked.forEach(function (file) { |
| 317 | var path = file.name; |
| 318 | if (fromFolder) { |
| 319 | var rel = file.webkitRelativePath || file.name; |
| 320 | var slash = rel.indexOf('/'); |
| 321 | path = slash >= 0 ? rel.slice(slash + 1) : rel; |
| 322 | } |
| 323 | if (hidden(path)) return; |
| 324 | if (supported.test(file.name)) { |
| 325 | notes.push({ file: file, path: path }); |
| 326 | } else { |
| 327 | media.push({ file: file, path: path, name: baseName(path) }); |
| 328 | } |
| 329 | }); |
| 330 | if (notes.length === 0) { |
| 331 | notes = media.map(function (item) { |
| 332 | return { |
| 333 | file: new Blob([ownText(item)]), |
| 334 | path: item.path.replace(/(\.[^.\/]*)?$/, '.md') |
| 335 | }; |
| 336 | }); |
| 337 | } |
| 338 | if (notes.length === 0) { |
| 339 | status.textContent = root.dataset.none; |
| 340 | link.classList.add('hidden'); |
| 341 | return; |
| 342 | } |
| 343 | run(notes, media); |
| 344 | }); |
| 345 | } |
| 346 | |
| 347 | onChange(files, false); |
| 348 | onChange(folder, true); |
| 349 | })(); |
| 350 | "#; |