Skip to content

Commit ea48b8b

Browse files
committed
preview page
1 parent 4797a39 commit ea48b8b

3 files changed

Lines changed: 63 additions & 0 deletions

File tree

build/build.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import io
1212
from urllib.parse import urljoin
1313
import shutil
14+
from tempfile import TemporaryDirectory
1415

1516
import markdown # type: ignore
1617
from tqdm import tqdm # type: ignore
@@ -136,6 +137,32 @@ def convert(self, md_content: str, relative_url: Path) -> str:
136137
return content
137138

138139

140+
def convert(request) -> str:
141+
request_json = request.get_json()
142+
143+
# prepare markdown converter
144+
extensions = [
145+
"fenced_code", # code blocks formatting
146+
"markdown_math_escape", # support for math formula
147+
"mdx_linkify", # convert text that looks like links into links
148+
]
149+
150+
with TemporaryDirectory() as tmpdirname:
151+
with open(tmpdirname + "/default.html", "w") as f:
152+
f.write("&text&")
153+
154+
md = MarkdownConverter(
155+
extensions=extensions,
156+
template_dir=Path(tmpdirname),
157+
baseurl="https://cp-algorithms.com"
158+
)
159+
160+
md_content = request_json['markdown']
161+
html_content = md.convert(md_content, Path("."))
162+
163+
return html_content
164+
165+
139166
def main():
140167
args = parse_arguments()
141168

src/_templates/preview.html

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<header>
4+
<meta charset="utf-8">
5+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
6+
<script>
7+
$(document).ready(function(){
8+
$("#previewBtn").click(function(){
9+
var markdown = $("#markdownInput").val();
10+
var URL = "https://us-central1-cp-algorithms.cloudfunctions.net/convert-markdown";
11+
$.post(URL, {"markdown": markdown}, function(data) {
12+
$("#previewArea").html(data);
13+
});
14+
});
15+
});
16+
</script>
17+
</header>
18+
<body>
19+
20+
<h1>Preview:</h1>
21+
22+
<form>
23+
<textarea id="markdownInput" rows="8" cols="80"></textarea>
24+
<br/>
25+
<button type='button' id="previewBtn">Preview</button>
26+
</form>
27+
<br/>
28+
<hr/>
29+
30+
<div id="previewArea">
31+
&text&
32+
</div>
33+
34+
</body>
35+
</html>

src/preview.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!--?template preview.html -->

0 commit comments

Comments
 (0)