Created
August 19, 2025 10:26
-
-
Save KoheiKanagu/850df7439ab75d82f20cfb7ae987656b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ==UserScript== | |
| // @name GitHub Markdown Image Table Generator | |
| // @namespace http://tampermonkey.net/ | |
| // @version 2025-08-19 | |
| // @description 選択したimgタグ群をMarkdownテーブル形式で横並びに変換するGitHub用ユーザースクリプト | |
| // @author KoheiKanagu | |
| // @match https://github.com/*/* | |
| // @icon https://www.google.com/s2/favicons?sz=64&domain=github.com | |
| // @grant GM_registerMenuCommand | |
| // ==/UserScript== | |
| (function () { | |
| "use strict"; | |
| // 選択テキスト内のimgタグ群をMarkdownテーブルに変換 | |
| function convertSelectedImgTagsToMarkdownTable(groupSize) { | |
| const activeElement = document.activeElement; | |
| // フォーカス・選択チェック削除 | |
| const start = activeElement.selectionStart; | |
| const end = activeElement.selectionEnd; | |
| const selectedText = activeElement.value.slice(start, end); | |
| // imgタグ抽出 | |
| const imgTagRegex = /(<img\s+[^>]+>)/g; | |
| const cells = []; | |
| let match; | |
| while ((match = imgTagRegex.exec(selectedText)) !== null) { | |
| const imgTag = match[1]; | |
| const srcMatch = imgTag.match(/src=["']([^"']+)["']/); | |
| const src = srcMatch ? srcMatch[1] : ""; | |
| cells.push(`<img src=\"${src}\" />`); | |
| } | |
| if (cells.length === 0) { | |
| return; | |
| } | |
| // 列数に合わせて複数行の同一テーブルとして出力 | |
| const colCount = groupSize; | |
| const rowCount = Math.ceil(cells.length / colCount); | |
| const header = "|" + Array(colCount).fill("").join("|") + "|\n"; | |
| const separator = "|" + Array(colCount).fill("-").join("|") + "|\n"; | |
| let tableRows = ""; | |
| for (let r = 0; r < rowCount; r++) { | |
| const rowCells = cells.slice(r * colCount, (r + 1) * colCount); | |
| while (rowCells.length < colCount) rowCells.push(""); | |
| tableRows += "| " + rowCells.join(" | ") + " |\n"; | |
| } | |
| const markdownTable = header + separator + tableRows; | |
| activeElement.value = | |
| activeElement.value.slice(0, start) + | |
| markdownTable + | |
| activeElement.value.slice(end); | |
| } | |
| // メニューコマンド登録 | |
| if (typeof GM_registerMenuCommand === "function") { | |
| GM_registerMenuCommand( | |
| "選択imgタグをMarkdownテーブル化(任意列数)", | |
| function () { | |
| let groupSize = parseInt( | |
| prompt("横に並べる画像の数を入力してください(2以上)", "3"), | |
| 10 | |
| ); | |
| if (isNaN(groupSize) || groupSize < 1) { | |
| alert("1以上の整数を入力してください。"); | |
| return; | |
| } | |
| convertSelectedImgTagsToMarkdownTable(groupSize); | |
| } | |
| ); | |
| } | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment