/* DokuWiki MoaiEditor Match_media.js file Version : 0.5 (May 5, 2026) Author : MoaiTools License : GPL 3 (http://www.gnu.org/licenses/gpl.html) */ MoaiEditor.MatchMedia = class { constructor(outer) { this.tools = outer; } // ──────────────────────────────────── same (score, block, node) { // Gather media tags in the text (if not cached) if (!('media' in block)) this.gatherFromText(block); // Gather media elements in the htlm element (if not cached) if (!('media' in node)) this.gatherFromNode(node); // Copy array because it will be trimmed var textMedia = block.media.slice(); // Check matches var n = 0; var m = 0; for (let pathname of node.media) { var i = 0; for (let words of textMedia) if (this.tools.wordsInString(words, pathname).score == 1) { m += 1; textMedia.splice(i,1); break; } n += 1; } // Update the score score.m += m; score.n += n; } // ──────────────────────────────────── gatherFromText (block) { // Get the text from the block var text = block.text; if ('cleantext' in block) text = block.cleantext; // Define patterns const patterns = [ /* /\[\{\{(?:[^\}]|(?:\}[^\}]))+\}\}\]/gu, // Imagebox plugin [{{ }}] */ /\{\{(?:[^\}]|(?:\}[^\}]))+\}\}/gu, // Bundled dokuwiki media tag {{wiki:dokuwiki-128.png?linkonly|Description}} ]; // Gather media tags in the text var media = []; for (let pattern of patterns) { const matches = text.match(pattern); if (matches === null) continue; // Remove the matches from the text to avoid double matching for (let match of matches) text = text.replace(match,''); // Parse the filename with namespace part for (let match of matches) { const filepath = match.match(/[^{]*?(?=[?|}])/); if (filepath !== null) { var parts = filepath[0].trim().split(/[:/\\]/); parts = parts.filter(str => (str !== '')); if (parts.length > 0) media.push(parts); } } } // Store block.media = media; block.cleantext = text; } // ──────────────────────────────────── gatherFromNode (node) { // Gather media files var pathnames = []; for (let mediaElement of node.handle.querySelectorAll(":scope > .media")) { // The filename can be either in 'src' or 'href' var pathname = null; const tag = mediaElement.tagName; if (tag == 'A') pathname = mediaElement.querySelector(":scope > img")?.src; if (tag == 'VIDEO') pathname = mediaElement.querySelector(":scope source")?.src; if (pathname !== undefined && pathname !== null) { pathnames.push(pathname); } } node.media = pathnames; } }; // End Class