Skip to content

Commit

Permalink
Improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
GaelGirodon committed Aug 6, 2023
1 parent 6b99873 commit c5792de
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 14 deletions.
4 changes: 2 additions & 2 deletions assets/features/code-copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
document.querySelectorAll("pre.code-block").forEach(function (codeBlock) {
// Wrap the <pre> tag with a <div>
var container = document.createElement("div");
container.classList = "code-container";
container.className = "code-container";
codeBlock.parentNode.insertBefore(container, codeBlock);
container.appendChild(codeBlock);
// Get the id of the textarea containing text to copy
var textarea = codeBlock.querySelector("textarea");
if (textarea) {
// Create the "Copy" button
var button = document.createElement("button");
button.classList = "btn-copy";
button.className = "btn-copy";
button.setAttribute("data-clipboard-target", "#" + textarea.id);
button.setAttribute("title", "Copy code block content");
button.innerText = "Copy";
Expand Down
14 changes: 14 additions & 0 deletions assets/features/numbered-headings.css
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
/*
* Numbered Headings in Markdown via CSS
* https://gist.github.com/patik/89ee6092c72a9e39950445c01598517a
*/

body {
counter-reset: h1counter h2counter h3counter h4counter h5counter h6counter;
}

h1 {
counter-reset: h2counter;
}

h2 {
counter-reset: h3counter;
}

h3 {
counter-reset: h4counter;
}

h4 {
counter-reset: h5counter;
}

h5 {
counter-reset: h6counter;
}

h6 {
}

Expand Down
4 changes: 3 additions & 1 deletion jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"moduleResolution": "node",
"checkJs": true,
"strict": true,
"moduleResolution": "node"
"noImplicitAny": false
},
"exclude": ["node_modules"]
}
2 changes: 2 additions & 0 deletions src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ export class Compiler {
.use(sup) // Superscript (<sup>) tag
.use(anchor, { level: 2 }) // Header anchors (permalinks)
.use(tocDoneRight, { level: [2, 3] }); // Table of contents

return this;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as files from "./files.js";
export class Extensions {
/**
* Construct the service from scripts paths.
* @param {Array} extensions Extension script paths
* @param {Array<*>} extensions Extension script paths
*/
constructor(extensions) {
this.extensionPaths = extensions || [];
Expand All @@ -26,7 +26,7 @@ export class Extensions {
throw new Error(`Invalid extension '${script}': file not found or not readable.`);
}
try {
this.extensions.push(await import(pathToFileURL(path.resolve(script))));
this.extensions.push(await import(pathToFileURL(path.resolve(script)).toString()));
} catch (e) {
throw new Error(`Invalid extension '${script}': not a valid ES Module (${e}).`);
}
Expand Down
2 changes: 1 addition & 1 deletion src/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export function readAllText(path) {
* If the target file already exists, it is overwritten.
* @param {string} path The path to the file to read
* @param {string} contents The string to write to the file.
* @return {Promise}
* @return {Promise<*>}
*/
export function writeAllText(path, contents) {
return fs.writeFile(path, contents, "utf8");
Expand Down
4 changes: 2 additions & 2 deletions src/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import https from "node:https";
* around Node.js http[s].request() to be used internally
* until the Fetch API reaches a stable state.
* @param {string} url HTTP(S) URL
* @param {boolean} failIfEmpty Throw an error if the fetched content is empty.
* @returns {Promise<{status:number,body:Buffer}>} Response with fetched content
* @param {boolean} [failIfEmpty] Throw an error if the fetched content is empty.
* @returns {Promise<{status?:number,body:Buffer}>} Response with fetched content
*/
export async function request(url, failIfEmpty) {
const u = new URL(url);
Expand Down
4 changes: 2 additions & 2 deletions src/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class Processor {
};
// Render output HTML
data = await this.extensions.exec("preRender", data);
let html = this.style.template(data);
let html = this.style.template?.(data) ?? "";
// Apply .code-block CSS class to all <pre> tags without class
html = html.replace(/<pre>/g, '<pre class="code-block">');
// Inline resources
Expand Down Expand Up @@ -151,7 +151,7 @@ export class Processor {
src = src.sort((a, b) => {
if (a.includes(b.replace(/(README|index)\.md$/gi, ""))) return 1;
if (b.includes(a.replace(/(README|index)\.md$/gi, ""))) return -1;
return a - b;
return a.localeCompare(b);
});
const base = path.dirname(src[0]); // Path to the base directory
let output = ""; // Output content
Expand Down
2 changes: 1 addition & 1 deletion src/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export class Style {

/**
* Convert a template using the legacy basic syntax to EJS for backward compatibility.
* @param template Template that can use the legacy basic template syntax
* @param {string} template Template that may use the legacy basic syntax
* @returns {string} EJS template
*/
legacyTemplateToEJS(template) {
Expand Down
4 changes: 2 additions & 2 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ export function randomId() {
*/
export function getHtmlTagText(html, tag) {
const startTagMatch = html?.match(new RegExp(`<${tag}[^>]*>`));
if (!(startTagMatch?.length > 0)) return null;
if (!startTagMatch || startTagMatch.index === undefined) return null;
const offset = startTagMatch.index + startTagMatch[0].length;
const endTagMatch = html.slice(offset).match(new RegExp(`</${tag}>`));
if (!(endTagMatch?.length > 0)) return null;
if (!endTagMatch?.input || endTagMatch.index === undefined) return null;
return endTagMatch.input
.slice(0, endTagMatch.index)
.replace(/<[^>]+>/g, "") // Remove nested tags
Expand Down
2 changes: 1 addition & 1 deletion test/processor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ describe("Processor", () => {
assert.isTrue(await files.exists(dst));
const html = await files.readAllText(dst);
// Numbered headings
assert.include(html, "<style>h1");
assert.include(html, "<style>body");
// Code copy
assert.include(html, "<textarea id=");
assert.include(html, "<script>/*!"); // clipboard.js
Expand Down

0 comments on commit c5792de

Please sign in to comment.