|
| 1 | +import { observer } from "mobx-react"; |
| 2 | +import { types } from "mobx-state-tree"; |
| 3 | +import ReactMarkdown from "react-markdown"; |
| 4 | + |
| 5 | +import Registry from "../../core/Registry"; |
| 6 | +import Tree from "../../core/Tree"; |
| 7 | +import ProcessAttrsMixin from "../../mixins/ProcessAttrs"; |
| 8 | +import VisibilityMixin from "../../mixins/Visibility"; |
| 9 | +import { AnnotationMixin } from "../../mixins/AnnotationMixin"; |
| 10 | +import { parseValue } from "../../utils/data"; |
| 11 | +import { guidGenerator } from "../../utils/unique"; |
| 12 | + |
| 13 | +// Custom markdown components with Tailwind styling |
| 14 | +const markdownComponents = { |
| 15 | + // Headings |
| 16 | + h1: ({ children }) => <h1 className="text-display-small font-bold mb-wide mt-wider">{children}</h1>, |
| 17 | + h2: ({ children }) => <h2 className="text-headline-large font-bold mb-base mt-wide">{children}</h2>, |
| 18 | + h3: ({ children }) => <h3 className="text-headline-medium font-semibold mb-base mt-wide">{children}</h3>, |
| 19 | + h4: ({ children }) => <h4 className="text-headline-small font-semibold mb-tight mt-base">{children}</h4>, |
| 20 | + h5: ({ children }) => <h5 className="text-title-large font-semibold mb-tight mt-base">{children}</h5>, |
| 21 | + h6: ({ children }) => <h6 className="text-title-medium font-semibold mb-tight mt-base">{children}</h6>, |
| 22 | + |
| 23 | + // Paragraphs |
| 24 | + p: ({ children }) => <p className="text-body-medium mb-base leading-body-medium">{children}</p>, |
| 25 | + |
| 26 | + // Lists |
| 27 | + ul: ({ children }) => <ul className="list-disc pl-base mb-base space-y-tighter">{children}</ul>, |
| 28 | + ol: ({ children }) => <ol className="list-decimal pl-base mb-base space-y-tighter">{children}</ol>, |
| 29 | + li: ({ children }) => <li className="text-body-medium pl-tight ml-base">{children}</li>, |
| 30 | + |
| 31 | + // Code |
| 32 | + code: ({ inline, children, ...props }) => { |
| 33 | + return inline ? ( |
| 34 | + <code |
| 35 | + className="bg-neutral-emphasis text-neutral-content px-tighter py-tightest rounded-smallest font-mono text-body-smaller" |
| 36 | + {...props} |
| 37 | + > |
| 38 | + {children} |
| 39 | + </code> |
| 40 | + ) : ( |
| 41 | + <code className="font-mono text-body-small" {...props}> |
| 42 | + {children} |
| 43 | + </code> |
| 44 | + ); |
| 45 | + }, |
| 46 | + pre: ({ children }) => ( |
| 47 | + <pre className="bg-neutral-surface-inset border border-neutral-border rounded-small p-base mb-base overflow-x-auto"> |
| 48 | + {children} |
| 49 | + </pre> |
| 50 | + ), |
| 51 | + |
| 52 | + // Blockquotes |
| 53 | + blockquote: ({ children }) => ( |
| 54 | + <blockquote className="border-l-4 border-primary-border pl-base ml-base mb-base italic text-neutral-content-subtle"> |
| 55 | + {children} |
| 56 | + </blockquote> |
| 57 | + ), |
| 58 | + |
| 59 | + // Links |
| 60 | + a: ({ children, href, ...props }) => ( |
| 61 | + <a href={href} className="text-primary-content hover:text-primary-content-hover underline" {...props}> |
| 62 | + {children} |
| 63 | + </a> |
| 64 | + ), |
| 65 | + |
| 66 | + // Horizontal rule |
| 67 | + hr: () => <hr className="border-neutral-border my-wide" />, |
| 68 | + |
| 69 | + // Tables |
| 70 | + table: ({ children }) => ( |
| 71 | + <div className="overflow-x-auto mb-base"> |
| 72 | + <table className="min-w-full border-collapse border border-neutral-border">{children}</table> |
| 73 | + </div> |
| 74 | + ), |
| 75 | + thead: ({ children }) => <thead className="bg-neutral-surface">{children}</thead>, |
| 76 | + tbody: ({ children }) => <tbody>{children}</tbody>, |
| 77 | + tr: ({ children }) => <tr className="border-b border-neutral-border">{children}</tr>, |
| 78 | + th: ({ children }) => ( |
| 79 | + <th className="border border-neutral-border px-base py-tight text-left font-semibold text-body-medium"> |
| 80 | + {children} |
| 81 | + </th> |
| 82 | + ), |
| 83 | + td: ({ children }) => <td className="border border-neutral-border px-base py-tight text-body-medium">{children}</td>, |
| 84 | + |
| 85 | + // Strong and emphasis |
| 86 | + strong: ({ children }) => <strong className="font-bold">{children}</strong>, |
| 87 | + em: ({ children }) => <em className="italic">{children}</em>, |
| 88 | + |
| 89 | + // Strikethrough |
| 90 | + del: ({ children }) => <del className="line-through text-neutral-content-subtle">{children}</del>, |
| 91 | +}; |
| 92 | + |
| 93 | +/** |
| 94 | + * The `Markdown` element is used to display markdown-formatted text content. |
| 95 | + * @example |
| 96 | + * <!-- Display markdown from task data --> |
| 97 | + * <View> |
| 98 | + * <Markdown value="$markdown_text"/> |
| 99 | + * </View> |
| 100 | + * @example |
| 101 | + * <!-- Display static markdown content; indents mark code blocks, so avoid them --> |
| 102 | + * <View> |
| 103 | + * <Markdown> |
| 104 | + * ## Instructions |
| 105 | + * |
| 106 | + * Please **carefully** read the following text and mark all entities. |
| 107 | + * </Markdown> |
| 108 | + * </View> |
| 109 | + * @example |
| 110 | + * <!-- Display markdown with custom styling --> |
| 111 | + * <View> |
| 112 | + * <Markdown value="$description" style="background: #f5f5f5; padding: 10px; border-radius: 4px;"/> |
| 113 | + * </View> |
| 114 | + * @name Markdown |
| 115 | + * @meta_title Markdown Tag for Rendering Markdown Text |
| 116 | + * @meta_description Customize Label Studio with the Markdown tag to display formatted markdown text content for machine learning and data science projects. |
| 117 | + * @param {string} value - Markdown text content, either static text or field name in task data (e.g., $markdown_field) |
| 118 | + * @param {string} [style] - CSS style string |
| 119 | + * @param {string} [className] - Class name of the CSS style to apply |
| 120 | + * @param {string} [idAttr] - Unique ID attribute to use in CSS |
| 121 | + * @param {region-selected|choice-selected|no-region-selected|choice-unselected} [visibleWhen] Control visibility of the content |
| 122 | + * @param {string} [whenTagName] Use with `visibleWhen`. Narrow down visibility by tag name |
| 123 | + * @param {string} [whenLabelValue] Use with `visibleWhen="region-selected"`. Narrow down visibility by label value |
| 124 | + * @param {string} [whenChoiceValue] Use with `visibleWhen` and `whenTagName`. Narrow down visibility by choice value |
| 125 | + */ |
| 126 | +const Model = types |
| 127 | + .model({ |
| 128 | + id: types.optional(types.identifier, guidGenerator), |
| 129 | + type: "markdown", |
| 130 | + value: types.optional(types.string, ""), |
| 131 | + _value: types.optional(types.string, ""), |
| 132 | + classname: types.optional(types.string, ""), |
| 133 | + style: types.maybeNull(types.string), |
| 134 | + idattr: types.optional(types.string, ""), |
| 135 | + }) |
| 136 | + .actions((self) => ({ |
| 137 | + updateValue(store) { |
| 138 | + const value = parseValue(self.value, store?.task?.dataObj ?? {}); |
| 139 | + |
| 140 | + // cut CDATA |
| 141 | + self._value = value.replace(/^\s*<!\[CDATA\[|\]\]>\s*$/g, ""); |
| 142 | + }, |
| 143 | + })); |
| 144 | + |
| 145 | +const MarkdownModel = types.compose("MarkdownModel", ProcessAttrsMixin, VisibilityMixin, AnnotationMixin, Model); |
| 146 | + |
| 147 | +const HtxMarkdown = observer(({ item }) => { |
| 148 | + const style = item.style ? Tree.cssConverter(item.style) : {}; |
| 149 | + |
| 150 | + if (item.isVisible === false) { |
| 151 | + style.display = "none"; |
| 152 | + } |
| 153 | + |
| 154 | + return ( |
| 155 | + <div id={item.idattr} className={item.classname} style={style}> |
| 156 | + <ReactMarkdown components={markdownComponents}>{item._value || ""}</ReactMarkdown> |
| 157 | + </div> |
| 158 | + ); |
| 159 | +}); |
| 160 | + |
| 161 | +Registry.addTag("markdown", MarkdownModel, HtxMarkdown); |
| 162 | + |
| 163 | +export { HtxMarkdown, MarkdownModel }; |
0 commit comments