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 committed Oct 23, 2024
1 parent c33c806 commit 4f883fa
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion client/src/components/GalaxyWizard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const props = defineProps({
const query = ref(props.query);
const queryResponse = ref("");
const busy = ref(false);
const { renderMarkdown } = useMarkdown({ openLinksInNewPage: true });
const { renderMarkdown } = useMarkdown({ openLinksInNewPage: true, removeNewlinesAfterList: true });
// on submit, query the server and put response in display box
function submitQuery() {
busy.value = true;
Expand Down
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 4f883fa

Please sign in to comment.