Skip to content

Commit

Permalink
Merge pull request #921 from WolframResearch/bugfix/454176
Browse files Browse the repository at this point in the history
Bugfix: Handle inline expression syntax in text
  • Loading branch information
rhennigan authored Nov 14, 2024
2 parents 4806f7c + bc781f6 commit af2e673
Showing 1 changed file with 39 additions and 6 deletions.
45 changes: 39 additions & 6 deletions Source/Chatbook/Formatting.wl
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ makeResultCell0[ mathCell[ math_String ] ] :=
]
];

makeResultCell0[ imageCell[ alt_String, url_String ] ] := image[ alt, url ];
makeResultCell0[ imageCell[ alt_, url_String ] ] := image[ alt, url ];

makeResultCell0[ hyperlinkCell[ label_String, url_String ] ] := hyperlink[ label, url ];

Expand Down Expand Up @@ -1283,7 +1283,13 @@ $stringFormatRules = {
Cell @ BoxData @ TemplateBox[
{ $freeformPromptBox, "paclet:guide/KnowledgeRepresentationAndAccess#203374175" },
"HyperlinkPaclet"
]
],

StringExpression[
"<!",
uri: (Repeated[ LetterCharacter, { 1, 20 } ] ~~ "://content-" ~~ Repeated[ LetterCharacter|DigitCharacter, 5 ]),
"!>"
] :> image[ None, uri ]
};

(* ::**************************************************************************************************************:: *)
Expand Down Expand Up @@ -2237,7 +2243,7 @@ image // beginDefinition;

image[ str_String ] := First @ StringSplit[ str, "![" ~~ alt__ ~~ "](" ~~ url__ ~~ ")" :> image[ alt, url ] ];

image[ alt_String, url_String ] := Enclose[
image[ alt_, url_String ] := Enclose[
Module[ { keys, key },
keys = ConfirmMatch[ Keys @ $attachments, { ___String? StringQ }, "Keys" ];
key = SelectFirst[ keys, StringContainsQ[ url, #1, IgnoreCase -> True ] & ];
Expand Down Expand Up @@ -2335,12 +2341,12 @@ attachmentBoxes // endDefinition;
(*markdownImageBoxes*)
markdownImageBoxes // beginDefinition;

markdownImageBoxes[ "", url_String, expr_ ] := PaneBox[
markdownImageBoxes[ ""|None, url_String, expr_ ] := PaneBox[
TagBox[
cachedBoxes @ expr,
"MarkdownImage",
AutoDelete -> True,
TaggingRules -> <| "CellToStringData" -> "![]("<>url<>")" |>
TaggingRules -> <| "CellToStringData" -> toMIBoxURI @ url |>
],
ImageMargins -> { { 0, 0 }, { 10, 10 } }
];
Expand All @@ -2350,13 +2356,40 @@ markdownImageBoxes[ alt_String, url_String, expr_ ] := PaneBox[
TooltipBox[ cachedBoxes @ expr, ToString[ alt, InputForm ] ],
"MarkdownImage",
AutoDelete -> True,
TaggingRules -> <| "CellToStringData" -> "!["<>alt<>"]("<>url<>")" |>
TaggingRules -> <| "CellToStringData" -> "![" <> alt <> "](" <> url <> ")" |>
],
ImageMargins -> { { 0, 0 }, { 10, 10 } }
];

markdownImageBoxes // endDefinition;

(* ::**************************************************************************************************************:: *)
(* ::Subsubsubsection::Closed:: *)
(*toMIBoxURI*)
toMIBoxURI // beginDefinition;

toMIBoxURI[ uri_String ] := Enclose[
Catch @ Module[ { expr },

expr = ConfirmMatch[
catchAlways @ Quiet @ GetExpressionURI[ uri, HoldComplete ],
HoldComplete[ _ ] | _Failure,
"GetExpression"
];

Replace[
expr,
{
HoldComplete[ e_ ] :> ConfirmBy[ MakeExpressionURI @ Unevaluated @ e, StringQ, "URI" ],
_Failure :> If[ StringQ @ URLParse[ uri, "Scheme" ], uri, "expression://"<>uri ]
}
]
],
throwInternalFailure
];

toMIBoxURI // endDefinition;

(* ::**************************************************************************************************************:: *)
(* ::Subsubsection::Closed:: *)
(*importedImage*)
Expand Down

0 comments on commit af2e673

Please sign in to comment.