@@ -3,6 +3,7 @@ import markmapScript from "./scripts/markmap.inline"
33import style from "./styles/markmap.scss"
44import { QuartzComponent , QuartzComponentConstructor , QuartzComponentProps } from "./types"
55import { classNames } from "../util/lang"
6+ import { FullSlug } from "../util/path"
67
78import { IPureNode } from 'markmap-common'
89
@@ -28,18 +29,26 @@ function replaceMatches(str: string, regex: RegExp, replacer: (match: RegExpExec
2829 return accumulator
2930}
3031
31- const wikilinkRegex = / \[ \[ (?< link > [ ^ | \] ] + ) \| ? ( (?< displayText > [ ^ \] ] + ) ) ? \] \] / g
32+ const wikilinkRegex = / ( ! ) ? \[ \[ (?< link > [ ^ | \] ] + ) \| ? (?< displayText > [ ^ \] ] * ) \] \] / g
33+ function replacement ( currentPath : FullSlug ) {
34+ return ( match : RegExpExecArray ) => {
35+ const { link, displayText } = match . groups !
36+ const isImage = / \. ( p n g | j p g | j p e g | g i f | s v g ) $ / . test ( link )
3237
33- function replacement ( match ) {
34- const { link, displayText } = match . groups !
35- const safeLink = `quartz-${ link . trim ( ) . replace ( / \s + / g, "-" ) } `
36- return `<a href=\"/${ safeLink } \">${ displayText || link } </a>`
38+ if ( isImage ) {
39+ return `<img src="../../../${ currentPath } /../attachments/${ link } " alt="${ displayText || link } " />`
40+ }
41+
42+ const safeLink = `quartz-${ link . trim ( ) . replace ( / \s + / g, "-" ) } `
43+ return `<a href="/${ safeLink } ">${ displayText || link } </a>`
44+ }
3745}
3846
39- export const parseInternalLinks = recurseChildren ( node => {
40- node . content =
41- replaceMatches ( node . content , wikilinkRegex , replacement )
42- } )
47+ export const parseInternalLinks = ( slug : FullSlug ) =>
48+ recurseChildren ( ( node ) => {
49+ node . content =
50+ replaceMatches ( node . content , wikilinkRegex , replacement ( slug ) )
51+ } )
4352
4453
4554const MarkmapViewer : QuartzComponent = ( { displayClass, fileData } : QuartzComponentProps ) => {
@@ -70,7 +79,7 @@ const MarkmapViewer: QuartzComponent = ({ displayClass, fileData }: QuartzCompon
7079 )
7180 }
7281
73- parseInternalLinks ( root )
82+ parseInternalLinks ( fileData . slug ! ) ( root )
7483
7584 return (
7685 < div class = { classNames ( displayClass , "markmap" ) } >
@@ -98,7 +107,12 @@ const MarkmapViewer: QuartzComponent = ({ displayClass, fileData }: QuartzCompon
98107 data-markmap = { encodeURIComponent ( JSON . stringify ( fileData . markmap ) ) }
99108 >
100109 < div class = "global-markmap-container" >
101- < svg id = "global-markmap" > </ svg >
110+ < svg
111+ id = "global-markmap"
112+ xmlns = "http://www.w3.org/2000/svg"
113+ width = "1240px"
114+ height = "1240px"
115+ > </ svg >
102116 < div id = "global-markmap-toolbar" > </ div >
103117 </ div >
104118 </ div >
0 commit comments