Skip to content

Commit

Permalink
Clean up and document.
Browse files Browse the repository at this point in the history
  • Loading branch information
LambdAurora committed May 22, 2024
1 parent 021f4be commit 23271e0
Show file tree
Hide file tree
Showing 14 changed files with 667 additions and 125 deletions.
2 changes: 1 addition & 1 deletion examples/file_render/file_render.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as md from "../../mod.ts";
import * as html from "@lambdaurora/libhtml";
import * as md from "../../mod.ts";
import katex from "https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.mjs"; // For inline LaTeX rendering

if (Deno.args.length !== 1) {
Expand Down
17 changes: 9 additions & 8 deletions examples/rendering_simple/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,28 @@ import Prism from "https://cdn.jsdelivr.net/npm/[email protected]/prism.min.js";
await fetch("./example.md")
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
throw new Error("Network response was not ok");
}
return response.text();
})
.then(text => {
let doc = md.parser.parse(text, {latex: false});
let doc = md.parser.parse(text, { latex: false });
md.render_to_dom(doc, document, {
block_code: {
highlighter: (code, language, parent) => {
if ((Prism.languages as any)[language]) {
const stuff = html.parse(
`<pre><code>${Prism.highlight(code, (Prism.languages as any)[language], language)}</code></pre>`
`<pre><code>${Prism.highlight(code, (Prism.languages as any)[language], language)}</code></pre>`,
) as html.Element;
parent.children = stuff.get_element_by_tag_name("code")!.children;
} else
} else {
parent.append_child(new html.Text(code));
}
}
},
},
image: {class_name: "responsive_img"},
spoiler: {enable: true},
parent: document.querySelector("main")
image: { class_name: "responsive_img" },
spoiler: { enable: true },
parent: document.querySelector("main"),
});

document.querySelectorAll(".spoiler_hidden").forEach(spoiler => {
Expand Down
File renamed without changes.
45 changes: 13 additions & 32 deletions lib/md/parser.ts → lib/parser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// deno-lint-ignore-file no-explicit-any
/*
* Copyright 2024 LambdAurora <[email protected]>
*
Expand All @@ -8,9 +9,9 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

import * as md from "./tree/index.ts";
import * as html from "@lambdaurora/libhtml";
import { HTML_TAGS_TO_PURGE_SUGGESTION, is_whitespace, purge_inline_html } from "../utils.ts";
import * as md from "./tree/index.ts";
import { HTML_TAGS_TO_PURGE_SUGGESTION, is_whitespace, purge_inline_html } from "./utils.ts";

/**
* Represents the parser options related to code elements.
Expand Down Expand Up @@ -325,7 +326,7 @@ function try_parse_url(input: string, start: number) {
* @param start the tag start index
* @param delimiter the tag delimiter (1 character)
* @param delimiter_repeat how many times the tag delimiter character is repeated to form the delimiter
* @return an object if the tag has been parsed successfully, else `null`
* @returns an object if the tag has been parsed successfully, else `null`
*/
function try_parse_tag(text: string, start: number, delimiter: string, delimiter_repeat: number) {
const rest = text.substring(start);
Expand Down Expand Up @@ -383,11 +384,12 @@ function try_parse_tag(text: string, start: number, delimiter: string, delimiter

/**
* Tries to parse a single tag by parsing with different delimiters through decreasing the delimiter repeat until a match is found.
*
* @param text the text to parse
* @param start the tag start index
* @param {string} delimiter the tag delimiter (1 character)
* @param {number} delimiter_repeat how many times the tag delimiter character is repeated to form the delimiter for the first iteration
* @return an object if the tag has been parsed successfully, or `null` otherwise
* @param delimiter the tag delimiter (1 character)
* @param delimiter_repeat how many times the tag delimiter character is repeated to form the delimiter for the first iteration
* @returns an object if the tag has been parsed successfully, or `null` otherwise
*/
function try_parse_possible_tags(text: string, start: number, delimiter: string, delimiter_repeat: number) {
for (let i = delimiter_repeat; i > 0; i--) {
Expand All @@ -399,7 +401,7 @@ function try_parse_possible_tags(text: string, start: number, delimiter: string,
return null;
}

function is_html_tag_allowed(tag: string, options: ParserOptions) {
function is_html_tag_allowed(tag: string, options: ParserOptions): boolean {
return !options.inline_html.disallowed_tags?.includes(tag);
}

Expand Down Expand Up @@ -493,7 +495,7 @@ function create_quote(block: BlockGroup, context: ParsingContext): md.BlockQuote
* Parses a Markdown document from the given string.
*
* @param string the Markdown source
* @param options the parser options
* @param options the parsing options
* @returns the parsed Markdown document
*/
export function parse(string: string, options: Partial<ParserOptions> = {}): md.Document {
Expand Down Expand Up @@ -790,31 +792,10 @@ function parse_block(block: BlockGroup, context: ParsingContext): md.BlockElemen
return new md.Comment(block.block.replace(/^\s*<!-?-?/, ""));
case "heading": {
// Heading
let nodes = block.block.split(" ");
let level;
switch (nodes[0].length) {
case 1:
level = md.HeadingLevel.H1;
break;
case 2:
level = md.HeadingLevel.H2;
break;
case 3:
level = md.HeadingLevel.H3;
break;
case 4:
level = md.HeadingLevel.H4;
break;
case 5:
level = md.HeadingLevel.H5;
break;
default:
level = md.HeadingLevel.H6;
break;
}
const nodes = block.block.split(" ");
nodes.shift();
const actual_nodes = parse_nodes(nodes.join(" "), false, context);
return new md.Heading(actual_nodes, level);
return new md.Heading(actual_nodes, Math.min(nodes[0].length, md.HeadingLevel.H6));
}
case "horizontal_rule":
return md.HORIZONTAL_RULE;
Expand Down Expand Up @@ -903,7 +884,7 @@ function parse_block(block: BlockGroup, context: ParsingContext): md.BlockElemen
level = indent[1].length / 2 >> 0;
}

for (let i = level; level > 0; level--) {
for (; level > 0; level--) {
if (current_list[level - 1]) {
break;
}
Expand Down
Loading

0 comments on commit 23271e0

Please sign in to comment.