Skip to content

Commit

Permalink
modified the mardown composable to process list items
Browse files Browse the repository at this point in the history
  • Loading branch information
tcollins2011 authored and dannon committed Nov 18, 2024
1 parent e7cebdc commit deca54e
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions client/src/composables/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,75 @@ function addRuleHeadingIncreaseLevel(engine: MarkdownIt, increaseBy: number) {
};
}

/**
* Add a rule that removes newlines after list items.
*/
function addRuleRemoveNewlinesAfterList(engine: MarkdownIt) {
const defaultRenderListItemOpen =
engine.renderer.rules.list_item_open ||
function (tokens, idx, options, _env, self) {
return self.renderToken(tokens, idx, options);
};

const defaultRenderListItemClose =
engine.renderer.rules.list_item_close ||
function (tokens, idx, options, _env, self) {
return self.renderToken(tokens, idx, options);
};

const defaultRenderOrderedListOpen =
engine.renderer.rules.ordered_list_open ||
function (tokens, idx, options, _env, self) {
return self.renderToken(tokens, idx, options);
};

const defaultRenderOrderedListClose =
engine.renderer.rules.ordered_list_close ||
function (tokens, idx, options, _env, self) {
return self.renderToken(tokens, idx, options);
};

const defaultRenderBulletListOpen =
engine.renderer.rules.bullet_list_open ||
function (tokens, idx, options, _env, self) {
return self.renderToken(tokens, idx, options);
};

const defaultRenderBulletListClose =
engine.renderer.rules.bullet_list_close ||
function (tokens, idx, options, _env, self) {
return self.renderToken(tokens, idx, options);
};

engine.renderer.rules.list_item_open = function (tokens, idx, options, env, self) {
return defaultRenderListItemOpen(tokens, idx, options, env, self).replace(/\n+$/, "");
};

engine.renderer.rules.list_item_close = function (tokens, idx, options, env, self) {
return defaultRenderListItemClose(tokens, idx, options, env, self).replace(/\n+$/, "");
};

engine.renderer.rules.ordered_list_open = function (tokens, idx, options, env, self) {
return defaultRenderOrderedListOpen(tokens, idx, options, env, self).replace(/\n+$/, "");
};

engine.renderer.rules.ordered_list_close = function (tokens, idx, options, env, self) {
return defaultRenderOrderedListClose(tokens, idx, options, env, self).replace(/\n+$/, "");
};

engine.renderer.rules.bullet_list_open = function (tokens, idx, options, env, self) {
return defaultRenderBulletListOpen(tokens, idx, options, env, self).replace(/\n+$/, "");
};

engine.renderer.rules.bullet_list_close = function (tokens, idx, options, env, self) {
return defaultRenderBulletListClose(tokens, idx, options, env, self).replace(/\n+$/, "");
};
}

interface UseMarkdownOptions {
openLinksInNewPage?: boolean;
increaseHeadingLevelBy?: number;
removeNewlinesAfterList?: boolean;
}

type RawMarkdown = string;
Expand All @@ -89,6 +155,10 @@ export function useMarkdown(options: UseMarkdownOptions = {}) {
addRuleHeadingIncreaseLevel(mdEngine, options.increaseHeadingLevelBy);
}

if (options.removeNewlinesAfterList) {
addRuleRemoveNewlinesAfterList(mdEngine);
}

function renderMarkdown(markdown: RawMarkdown): HTMLString {
return mdEngine.render(markdown);
}
Expand Down

0 comments on commit deca54e

Please sign in to comment.