1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 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
| #Requires AutoHotkey v1.1+ #IfWinActive ahk_exe Typora.exe
; Alt+1 红 !1::addOrReplaceFontColor("red") ; Alt+2 绿色 !2::addOrReplaceFontColor("#10c300") ; Alt+3 浅蓝 !3::addOrReplaceFontColor("cornflowerblue") ; Alt+4 橙 !4::addOrReplaceFontColor("orange") ; Alt+W 去除颜色 !w::removeFontColor() ; Alt+Q 插入换行标签<br> !q:: SendInput {Text}<br> return
#IfWinActive
;-------------------------------------- ; 添加或替换颜色标签 ;-------------------------------------- addOrReplaceFontColor(color) { clipboard := "" ; 清空剪贴板 ; 选中整行 Send {Home} Send +{End} Send ^c ClipWait, 0.3
if (clipboard != "") { text := clipboard ; 检查是否已有 **<span style='color:...'>...** if RegExMatch(text, "^\*\*<span\s+style=['""]color:\s*([^'"";]+);?['""]>(.*)</span>\*\*$", m) { innerText := m2 ; 去掉旧的 markdown ** 和 span 标签 innerText := RegExReplace(innerText, "\*\*", "") ; 直接替换颜色,并保持 markdown 加粗 text := "**<span style='color:" . color . "'>" . innerText . "</span>**" } else { ; 没有标签则添加 ** + span ; 去掉旧的 ** text := RegExReplace(text, "\*\*", "") ; 去掉旧的 font 或 span text := RegExReplace(text, "<font[^>]*>", "") text := RegExReplace(text, "</font>", "") text := RegExReplace(text, "<span[^>]*>", "") text := RegExReplace(text, "</span>", "") text := "**<span style='color:" . color . "'>" . text . "</span>**" } clipboard := text Send ^v Send {End} } else { ; 空行时插入标签,加粗占位 newText := "**<span style='color:" . color . "'></span>**" SendInput {Text}%newText% ; 光标定位到 span 内部 Send {Left 7} ; 把光标移到 </span> 前 } }
removeFontColor() { clipboard := "" ; 选中整行 Send {Home} Send +{End} Send ^c ClipWait, 0.3
if (clipboard != "") { cleaned := clipboard ; 去掉 <font> 和 <span> 标签 cleaned := RegExReplace(cleaned, "<font[^>]*>", "") cleaned := RegExReplace(cleaned, "</font>", "") cleaned := RegExReplace(cleaned, "<span[^>]*>", "") cleaned := RegExReplace(cleaned, "</span>", "") ; 去掉 Markdown 粗体 ** cleaned := RegExReplace(cleaned, "\*\*", "") clipboard := cleaned Send ^v Send {End} } }
|