Skip to content

Commit 6354d6d

Browse files
Change the markdown preview to a toggle button
1 parent 3efbf11 commit 6354d6d

File tree

1 file changed

+45
-23
lines changed

1 file changed

+45
-23
lines changed

src/components/MarkdownPreview.svelte

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
let renderedOutput = "";
88
let error = "";
99
let highlighter;
10+
let isPreviewMode = false; // New state to track the current mode
1011
1112
onMount(async () => {
1213
try {
@@ -27,7 +28,13 @@
2728
],
2829
});
2930
30-
// Configure marked to use Shiki
31+
marked.use({
32+
pedantic: false,
33+
gfm: true,
34+
breaks: true,
35+
});
36+
37+
// Configure marked with all options
3138
marked.setOptions({
3239
highlight: (code, lang) => {
3340
try {
@@ -88,6 +95,14 @@
8895
URL.revokeObjectURL(url);
8996
}
9097
98+
// Function to toggle between editor and preview modes
99+
function toggleMode() {
100+
isPreviewMode = !isPreviewMode;
101+
if (isPreviewMode) {
102+
updatePreview();
103+
}
104+
}
105+
91106
onDestroy(() => {
92107
if (highlighter) {
93108
highlighter.dispose();
@@ -96,32 +111,39 @@
96111
</script>
97112

98113
<div class="markdown-preview">
99-
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
100-
<div class="editor">
101-
<textarea
102-
class="w-full h-[500px] p-4 border border-gray-200 rounded-sm"
103-
bind:value={markdownInput}
104-
on:input={updatePreview}
105-
placeholder="Type your markdown here..."
106-
></textarea>
107-
</div>
108-
<div class="preview">
109-
<div class="w-full h-[500px] p-4 border border-gray-200 rounded-sm overflow-auto">
110-
{@html renderedOutput}
111-
</div>
114+
<div class="flex flex-col md:flex-row gap-2 justify-between items-center mb-4">
115+
<button
116+
class="px-2 py-2 border-2 rounded-full hover:bg-purple-300 dark:hover:bg-purple-600 dark:text-slate-100"
117+
on:click={toggleMode}
118+
>
119+
{isPreviewMode ? "Switch to Editor" : "Switch to Preview"}
120+
</button>
121+
<div class="flex flex-col md:flex-row gap-2">
122+
<button
123+
class="px-2 py-2 border-2 rounded-full hover:bg-purple-300 dark:hover:bg-purple-600 dark:text-slate-100"
124+
on:click={copyToClipboard}>Copy to Clipboard</button
125+
>
126+
<button
127+
class="px-2 py-2 border-2 rounded-full hover:bg-purple-300 dark:hover:bg-purple-600 dark:text-slate-100"
128+
on:click={downloadMarkdown}>Download as .md</button
129+
>
112130
</div>
113131
</div>
114132

115-
<div class="flex gap-2 mt-4">
116-
<button
117-
class="px-4 py-2 border-2 rounded-full hover:bg-purple-300 dark:hover:bg-purple-600 dark:text-slate-100"
118-
on:click={copyToClipboard}>Copy to Clipboard</button
119-
>
120-
<button
121-
class="px-4 py-2 border-2 rounded-full hover:bg-purple-300 dark:hover:bg-purple-600 dark:text-slate-100"
122-
on:click={downloadMarkdown}>Download as .md</button
133+
{#if isPreviewMode}
134+
<div
135+
class="preview not-prose w-full h-[500px] p-4 border border-gray-200 rounded-sm overflow-auto"
123136
>
124-
</div>
137+
{@html renderedOutput}
138+
</div>
139+
{:else}
140+
<textarea
141+
class="w-full h-[500px] p-4 border border-gray-200 rounded-sm"
142+
bind:value={markdownInput}
143+
on:input={updatePreview}
144+
placeholder="Type your markdown here..."
145+
></textarea>
146+
{/if}
125147

126148
{#if error}
127149
<div class="error mt-4 text-red-500">{error}</div>

0 commit comments

Comments
 (0)