Skip to content

Commit

Permalink
Merge pull request #82 from admisio/marked_highlight
Browse files Browse the repository at this point in the history
(backend) (frontend) parse time highlighting
  • Loading branch information
EETagent authored May 29, 2023
2 parents 1a8912a + cfd0969 commit 37d14c7
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 54 deletions.
7 changes: 4 additions & 3 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
"bcrypt": "^5.1.0",
"date-fns": "^2.29.3",
"debounce": "^1.2.1",
"marked": "^4.3.0",
"marked": "^5.0.0",
"marked-highlight": "^1.0.1",
"cheerio": "1.0.0-rc.12",
"highlight.js": "^11.7.0",
"highlight.js": "^11.8.0",
"jsonwebtoken": "^9.0.0",
"trpc-sveltekit": "^3.5.1"
},
Expand All @@ -33,7 +34,7 @@
"@types/bcrypt": "^5.0.0",
"@types/debounce": "^1.2.1",
"@types/jsonwebtoken": "^9.0.1",
"@types/marked": "^4.0.8",
"@types/marked": "^4.3.0",
"@types/node": "18.15.5",
"svelte": "^3.57.0",
"svelte-check": "^3.1.4",
Expand Down
8 changes: 0 additions & 8 deletions apps/web/src/lib/components/testview/TestResult.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,12 @@
let isDarkMode: boolean = false;
import hljs from 'highlight.js';
import 'highlight.js/styles/github-dark.css';
import { onMount } from 'svelte';
import type { Answer, Heading, Prisma, Question, Submission } from '@testy/database';
import Answers from './Answers.svelte';
import { getInBetweenQuestionRows } from '$lib/utils/headings';
onMount(() => {
document.querySelectorAll('.description code').forEach((el) => {
hljs.highlightElement(el as HTMLElement);
});
});
</script>

<div
Expand Down
12 changes: 12 additions & 0 deletions apps/web/src/lib/utils/mdParser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { marked } from 'marked';
import { markedHighlight } from 'marked-highlight';
import hljs from 'highlight.js';

import { load } from 'cheerio';
import type { HeadingType, TemplateType, Question } from '@testy/trpc';

Expand Down Expand Up @@ -38,6 +41,15 @@ const parseQuestion = (questionHTML: string): Question => {
};

export const parseMd = async (md: string, timeLimit: number): Promise<TemplateType> => {
marked.use(
markedHighlight({
langPrefix: 'hljs language-',
highlight(code: string, lang: string) {
const language = hljs.getLanguage(lang) ? lang : 'plaintext';
return hljs.highlight(code, { language }).value;
}
})
);
const html = marked.parse(md);
console.log(html);
const document = load(html);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
let isDarkMode: boolean = false;
import hljs from 'highlight.js';
import 'highlight.js/styles/github-dark.css';
import { onDestroy, onMount } from 'svelte';
Expand All @@ -51,16 +50,6 @@
import { getInBetweenQuestionRows } from '$lib/utils/headings';
onMount(() => {
document.querySelectorAll('.description code').forEach((el) => {
// create pre element and insert it before the code element and put the code element inside the pre element
const pre = document.createElement('pre');
pre.className = 'hljs';
el.parentNode?.insertBefore(pre, el);
pre.appendChild(el);
hljs.highlightElement(el as HTMLElement);
});
updateTimeRemaining();
updateTimeRemainingInterval = setInterval(() => updateTimeRemaining(), 1000);
});
Expand Down Expand Up @@ -156,7 +145,7 @@
{/if}
{#if heading && heading.description}
<p
class="mt-4 text-ellipsis break-words text-justify text-xl font-serif dark:text-gray-400"
class="mt-4 text-ellipsis break-words text-justify font-serif text-xl dark:text-gray-400"
>
{@html heading.description}
</p>
Expand Down Expand Up @@ -203,7 +192,7 @@

<style lang="postcss">
.title-wrapper :global(code) {
@apply rounded-md bg-[#1D1D1E] py-1 px-1 text-[#D4D4D4];
@apply rounded-md bg-[#1D1D1E] px-1 py-1 text-[#D4D4D4];
}
.endTimeFixed {
@apply fixed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,13 @@
export let data: PageData;
const test = data.template!;
import hljs from 'highlight.js';
import 'highlight.js/styles/github.css';
import { onMount } from 'svelte';
import TestPreview from '$lib/components/testpreview/TestPreview.svelte';
onMount(() => {
document.querySelectorAll('.description code').forEach((el) => {
hljs.highlightElement(el as HTMLElement);
});
});
</script>

<div
class="<md:flex-col mx-auto mx-auto mb-6 flex max-w-screen-xl items-center px-4 px-4 py-3 md:px-6 md:px-6"
class="<md:flex-col mx-auto mx-auto mb-6 flex max-w-screen-xl items-center px-4 px-4 py-3 md:px-6 md:px-6"
>
<h1 class="<md:mb-3 text-6xl font-bold text-[#3580b7]">Úprava testu / {test.title}</h1>
</div>
Expand Down
8 changes: 0 additions & 8 deletions apps/web/src/routes/preview/template/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
<script lang="ts">
import { onMount } from 'svelte';
import { enhance } from '$app/forms';
import hljs from 'highlight.js';
import 'highlight.js/styles/github.css';
import type { ActionData } from './$types';
import TestPreview from '$lib/components/testpreview/TestPreview.svelte';
export let form: ActionData;
onMount(() => {
document.querySelectorAll('.description code').forEach((el) => {
hljs.highlightElement(el as HTMLElement);
});
});
</script>

<form method="POST" use:enhance>
Expand Down
37 changes: 24 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 37d14c7

Please sign in to comment.