Skip to content

Commit

Permalink
Add JSDoc comments
Browse files Browse the repository at this point in the history
  • Loading branch information
vrugtehagel committed Jun 16, 2024
1 parent 7d76384 commit 9877411
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vrugtehagel/eleventy-document-outline",
"version": "0.0.2",
"version": "0.0.3",
"exports": "./mod.ts",
"publish": {
"include": ["mod.ts", "README.md"]
Expand Down
17 changes: 16 additions & 1 deletion mod.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,49 @@
import * as HTMLParser from "npm:node-html-parser@^6.1";

/** Allowed header tag names, lowercased */
const HEADERS: string[] = ["h1", "h2", "h3", "h4", "h5", "h6", "h7"] as const;
/** Default options, used if not provided explicitly */
const DEFAULTS: EleventyDocumentOutlineOptions = {
headers: ["h1", "h2", "h3"],
output: ({ id, text, header }) =>
`<a href="#${id}" class="link-${header}">${text}</a>`,
};

/** The Eleventy config object, should be a better type than "any" but alas */
type EleventyConfig = any;
/** UUIDs are rendered by the shortcode and then replaced in a transform at build time */
type UUID = string;
/** Allowed header tag names, lowercased, as a type */
type LowerCaseHeader = typeof HEADERS[number];
/** Allowed header tag names, uppercased */
type UpperCaseHeader = "H1" | "H2" | "H3" | "H4" | "H5" | "H6" | "H7";
/** Allowed header tag names, case-insensitive */
type Header = LowerCaseHeader | UpperCaseHeader;
/** The options. All fields are optional through Partial<…> */
type EleventyDocumentOutlineOptions = {
headers: Header[];
output: (info: { id: string; text: string; header: Header }) => string;
};

/**
* Receives a user-provided header and returns a normalized (lowercased) header.
* Throws if the argument is not a valid header.
*/
function normalize(header: string): LowerCaseHeader {
const lowerCase = header.toLowerCase();
if (HEADERS.includes(lowerCase)) return lowerCase;
throw new Error(`Invalid header "${header}" found.`);
}

/** The plugin itself, with an optional options object as second argument. */
export default function EleventyDocumentOutline(
config: EleventyConfig,
options: Partial<EleventyDocumentOutlineOptions> = {},
options?: Partial<EleventyDocumentOutlineOptions> = {},
) {
const normalizedOptions = Object.assign({}, DEFAULTS, options);
const memory = new Map<UUID, LowerCaseHeader[]>();

/** The {% outline %} shortcode, with optional configurable headers */
config.addShortcode("outline", function (...headers: Header[]) {
const uuid = crypto.randomUUID();
const normalized = headers.length == 0
Expand All @@ -39,6 +53,7 @@ export default function EleventyDocumentOutline(
return uuid;
});

/** The transform responsible for actually generating and rendering the links */
config.addTransform(
"document-outline",
async function (this: any, content: string) {
Expand Down

0 comments on commit 9877411

Please sign in to comment.