Skip to content

Commit

Permalink
[#69922] make a generic location for directives and allow captionless…
Browse files Browse the repository at this point in the history
… figures
  • Loading branch information
Trzcin authored and mgielda committed Dec 9, 2024
1 parent 6d52772 commit cb5b1bf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
49 changes: 28 additions & 21 deletions src/hooks/markdownFigureMd.js → src/hooks/markdownDirectives.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const shared_option_spec = {
name: directiveOptions.unchanged,
};

export class FigureMd extends directivesDefault.image {
class FigureMd extends directivesDefault.image {
option_spec = {
...shared_option_spec,
align: directiveOptions.create_choice(["left", "center", "right"]),
Expand Down Expand Up @@ -62,30 +62,33 @@ export class FigureMd extends directivesDefault.image {
imageToken.attrJoin("class", data.options.class.join(" "));
}

const [caption, ...legendParts] = data.body.split("\n\n").slice(1);
const legend = legendParts.join("\n\n");
const captionMap = data.bodyMap[0] + 2;
const openCaption = this.createToken("figure_caption_open", "figcaption", 1, {
block: true,
});
if (target) {
openCaption.attrSet("number", `${target.number}`);
}
const captionBody = this.nestedParse(caption, captionMap);
const closeCaption = this.createToken("figure_caption_close", "figcaption", -1, {
block: true,
});
captionTokens = [openCaption, ...captionBody, closeCaption];
if (legend) {
const legendMap = captionMap + caption.split("\n").length + 1;
const openLegend = this.createToken("figure_legend_open", "", 1, {
const captionSplit = data.body.split("\n\n");
if (captionSplit.length > 1) {
const [caption, ...legendParts] = captionSplit.slice(1);
const legend = legendParts.join("\n\n");
const captionMap = data.bodyMap[0] + 2;
const openCaption = this.createToken("figure_caption_open", "figcaption", 1, {
block: true,
});
const legendBody = this.nestedParse(legend, legendMap);
const closeLegend = this.createToken("figure_legend_close", "", -1, {
if (target) {
openCaption.attrSet("number", `${target.number}`);
}
const captionBody = this.nestedParse(caption, captionMap);
const closeCaption = this.createToken("figure_caption_close", "figcaption", -1, {
block: true,
});
legendTokens = [openLegend, ...legendBody, closeLegend];
captionTokens = [openCaption, ...captionBody, closeCaption];
if (legend) {
const legendMap = captionMap + caption.split("\n").length + 1;
const openLegend = this.createToken("figure_legend_open", "", 1, {
block: true,
});
const legendBody = this.nestedParse(legend, legendMap);
const closeLegend = this.createToken("figure_legend_close", "", -1, {
block: true,
});
legendTokens = [openLegend, ...legendBody, closeLegend];
}
}
}
const closeToken = this.createToken("figure_close", "figure", -1, { block: true });
Expand Down Expand Up @@ -136,3 +139,7 @@ function getNamespacedMeta(token) {
if (!token.meta.docutils) token.meta.docutils = meta;
return meta;
}

export default {
'figure-md': FigureMd
}
4 changes: 2 additions & 2 deletions src/hooks/useText.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { useComputed } from "@preact/signals";
import markdownCheckboxes from "markdown-it-checkbox";
import { colonFencedBlocks } from "./markdownFence";
import { markdownItMapUrls } from "./markdownUrlMapping";
import { FigureMd } from "./markdownFigureMd";
import newDirectives from "./markdownDirectives";

const countOccurences = (str, pattern) => (str?.match(pattern) || []).length;

Expand Down Expand Up @@ -107,7 +107,7 @@ export const useText = ({ preview }) => {

const markdown = useComputed(() => {
const md = markdownIt({ breaks: true, linkify: true, html: true })
.use(markdownitDocutils, { directives: { ...directivesDefault, "figure-md": FigureMd } })
.use(markdownitDocutils, { directives: { ...directivesDefault, ...newDirectives } })
.use(markdownReplacer(options.transforms.value, options.parent, cache.transform))
.use(useCustomRoles(options.customRoles.value, options.parent, cache.transform))
.use(useCustomDirectives(options.customDirectives.value, options.parent, cache.transform))
Expand Down

0 comments on commit cb5b1bf

Please sign in to comment.