Skip to content

Commit 46e5f6a

Browse files
committed
Fixed the image bug
1 parent 6f3143f commit 46e5f6a

3 files changed

Lines changed: 35 additions & 1 deletion

File tree

markopolis/app.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from fastapi import FastAPI, HTTPException, Depends, Header, Path, Request, Query
44
from fastapi.staticfiles import StaticFiles
55
from fastapi.templating import Jinja2Templates
6-
from fastapi.responses import JSONResponse, HTMLResponse
6+
from fastapi.responses import JSONResponse, HTMLResponse, FileResponse
77
from fastapi.middleware.cors import CORSMiddleware
88
import markopolis.dantic as D
99
import markopolis.md as M
@@ -189,6 +189,17 @@ async def get_note_html(
189189

190190
@app.get("/{path:path}", response_class=HTMLResponse)
191191
async def load_page(request: Request, path: str):
192+
_cond = (
193+
path.endswith(".png")
194+
or path.endswith(".jpg")
195+
or path.endswith(".jpeg")
196+
or path.endswith(".gif")
197+
or path.endswith(".svg")
198+
or path.endswith(".webp")
199+
)
200+
if _cond:
201+
img_pth = os.path.join(settings.md_path, path)
202+
return FileResponse(img_pth)
192203
try:
193204
frontmatter = M.get_frontmatter(path)
194205
html_content = M.get_note_html(path)

markopolis/md.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import sh
77
import markdown
88
from .md_extensions import (
9+
ImageExtension,
910
CalloutExtension,
1011
MermaidExtension,
1112
StrikethroughExtension,
@@ -210,6 +211,7 @@ def get_note_html(note_path):
210211
extensions=[
211212
"fenced_code",
212213
"codehilite",
214+
ImageExtension(),
213215
"mdx_wikilink_plus",
214216
# WikiLinkExtension(base_url="/", end_url=""),
215217
"markdown_checklist.extension",

markopolis/md_extensions.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,27 @@
77
import re
88

99

10+
class ImageExtension(markdown.extensions.Extension):
11+
def extendMarkdown(self, md):
12+
# Register the preprocessor
13+
md.preprocessors.register(ImagePreprocessor(md), "image_preprocessor", 25)
14+
15+
16+
class ImagePreprocessor(Preprocessor):
17+
# Pattern for image extensions (webp, jpg, jpeg, png, svg)
18+
IMAGE_PATTERN = re.compile(
19+
r"!\[\[(.*?\.(?:webp|jpg|jpeg|png|svg))\]\]"
20+
) # Custom ![[image.ext]]
21+
22+
def run(self, lines):
23+
new_lines = []
24+
for line in lines:
25+
# Substitute custom image syntax ![[image.ext]] with standard Markdown image syntax ![](image.ext)
26+
new_line = re.sub(self.IMAGE_PATTERN, r"![](\1)", line)
27+
new_lines.append(new_line)
28+
return new_lines
29+
30+
1031
# Custom inline pattern for obsidian style images
1132
class ObsidianImageInlineProcessor(InlineProcessor):
1233
def handleMatch(self, m, data):

0 commit comments

Comments
 (0)