From e96b0c0bced2a75b3cbcbde4687e63a17a918974 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E7=8E=8B=E5=81=A5=E8=BE=89?=
<40563566+mrwangjianhui@users.noreply.github.com>
Date: Thu, 28 Jul 2022 13:40:56 +0800
Subject: [PATCH] repair empty md wiki mode display (#5222)
* repair empty md display
* optimize code
---
seahub/wiki/views.py | 144 +++++++++++++++++++++++--------------------
1 file changed, 78 insertions(+), 66 deletions(-)
diff --git a/seahub/wiki/views.py b/seahub/wiki/views.py
index 025ed4e3bb..d4aae19cf9 100644
--- a/seahub/wiki/views.py
+++ b/seahub/wiki/views.py
@@ -28,6 +28,76 @@ from seahub.settings import SERVICE_URL
logger = logging.getLogger(__name__)
+def format_markdown_file_content(slug, repo_id, file_path, token, file_response):
+ # Convert a markdown string to HTML and parse the html
+ try:
+ html_content = markdown.markdown(file_response)
+ html_doc = lxml.html.fromstring(html_content)
+ except Exception as err_msg:
+ return '', [], err_msg
+
+ service_url = SERVICE_URL.strip('/')
+
+ # Replace
src to wiki mode
+ img_labels = html_doc.xpath('//img') # Get all
labels
+ img_url_re = re.compile(r'^%s/lib/%s/file/.*raw=1$' % (service_url, repo_id))
+ for img in img_labels:
+ img_url = img.attrib.get('src', '')
+ if img_url_re.match(img_url) is not None:
+ img_path = img_url[img_url.find('/file/')+5:img_url.find('?')]
+ new_img_url = '%s/view-image-via-public-wiki/?slug=%s&path=%s' % (service_url, slug, img_path)
+ html_content = html_content.replace(img_url, new_img_url)
+ elif re.compile(r'^\.\./*|^\./').match(img_url):
+ if img_url.startswith('../'):
+ img_path = os.path.join(os.path.dirname(os.path.dirname(file_path)), img_url[3:])
+ else:
+ img_path = os.path.join(os.path.dirname(file_path), img_url[2:])
+ new_img_url = '%s/view-image-via-public-wiki/?slug=%s&path=%s' % (service_url, slug, img_path)
+ html_content = html_content.replace(img_url, new_img_url)
+
+ # Replace href to wiki mode
+ a_labels = html_doc.xpath('//a') # Get all labels
+ file_url_re = re.compile(r'^%s/lib/%s/file/.*' % (service_url, repo_id))
+ md_url_re = re.compile(r'^%s/lib/%s/file/.*\.md$' % (service_url, repo_id))
+ dir_url_re = re.compile(r'^%s/library/%s/(.*)' % (service_url, repo_id))
+ for a in a_labels:
+ old_href = a.attrib.get('href', '')
+ if file_url_re.match(old_href) is not None:
+ path = old_href[old_href.find('/file/') + 5:].strip('/')
+ if md_url_re.match(old_href) is not None:
+ new_href = '%s/published/%s/%s' % (service_url, slug, path)
+ html_content = html_content.replace(old_href, new_href)
+ else:
+ new_href = '%s/d/%s/files/?p=%s&dl=1' % (service_url, token, path)
+ html_content = html_content.replace(old_href, new_href)
+ elif dir_url_re.match(old_href) is not None:
+ url_path = dir_url_re.match(old_href).groups()[0].strip('/')
+ path = url_path[url_path.find('/'):].strip('/')
+ new_href = '%s/published/%s/%s' % (service_url, slug, path)
+ html_content = html_content.replace(old_href, new_href)
+
+ # Get markdown outlines and format labels
+ outlines = list()
+ for p in html_content.split('\n'):
+ if p.startswith('') and p.endswith('
'):
+ head = p.replace('