@@ -15,9 +15,8 @@ import { computed, onBeforeUnmount, onMounted, ref, watch, nextTick } from 'vue'
1515import { ipc , store } from ' @/electron'
1616import { marked } from ' marked'
1717import mermaid from ' mermaid'
18- import { useHljsTheme } from ' @/composable'
18+ import { useHljsTheme , goToSnippet } from ' @/composable'
1919import { useCodemirror } from ' @/composable/codemirror'
20-
2120import { nanoid } from ' nanoid'
2221
2322const isDev = import .meta .env .DEV
@@ -75,7 +74,12 @@ const init = () => {
7574 }
7675 },
7776 link (href : string , title : string , text : string ) {
78- return ` <a href="${href }" class="external">${text }</a> `
77+ if (/ ^ masscode:\/\/ snippets/ .test (href )) {
78+ const id = href .split (' /' ).pop ()
79+ return ` <a href="${href }" class="snippet-link" data-snippet-id="${id }">${text }</a> `
80+ } else {
81+ return ` <a href="${href }" class="external">${text }</a> `
82+ }
7983 }
8084 }
8185
@@ -181,9 +185,11 @@ const render = () => {
181185 ' type' ,
182186 ' checked' ,
183187 ' disabled' ,
184- ' id'
188+ ' id' ,
189+ ' data-*'
185190 ]
186- }
191+ },
192+ allowedSchemes: [' http' , ' https' , ' masscode' ]
187193 })
188194
189195 const re = / src="\.\/ / g
@@ -196,12 +202,18 @@ const render = () => {
196202 renderedHtml .value = html
197203}
198204
199- const openExternal = (e : Event ) => {
205+ const onLink = async (e : Event ) => {
200206 const el = e .target as HTMLAnchorElement
201207 e .preventDefault ()
208+
202209 if (el .classList .contains (' external' )) {
203210 ipc .invoke (' main:open-url' , el .href )
204211 }
212+
213+ if (el .classList .contains (' snippet-link' )) {
214+ const { snippetId } = el .dataset
215+ if (snippetId ) goToSnippet (snippetId )
216+ }
205217}
206218
207219const height = computed (() => {
@@ -257,11 +269,11 @@ watch(
257269init ()
258270
259271onMounted (() => {
260- document .addEventListener (' click' , openExternal )
272+ document .addEventListener (' click' , onLink )
261273})
262274
263275onBeforeUnmount (() => {
264- document .removeEventListener (' click' , openExternal )
276+ document .removeEventListener (' click' , onLink )
265277})
266278
267279window .addEventListener (' resize' , () => {
0 commit comments