Skip to content

Commit

Permalink
[asciidoc] tolerate inline link in code block (weirdish)
Browse files Browse the repository at this point in the history
  • Loading branch information
rmannibucau committed Dec 30, 2023
1 parent 106e54a commit 808d59c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,13 @@
import static io.yupiik.asciidoc.model.Text.Style.SUB;
import static io.yupiik.asciidoc.model.Text.Style.SUP;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Map.entry;
import static java.util.Optional.empty;
import static java.util.Optional.of;
import static java.util.Optional.ofNullable;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toMap;
import static java.util.stream.Collectors.toUnmodifiableMap;

/**
Expand Down Expand Up @@ -769,7 +771,22 @@ private List<Element> parseLine(final Reader reader, final String line,
if (start != i) {
flushText(elements, line.substring(start, i));
}
elements.add(new Code(line.substring(i + 1, end), List.of(), Map.of(), true));
final var content = line.substring(i + 1, end);
if (isLink(content)) { // this looks like a bad practise but can happen
final var link = unwrapElementIfPossible(parseParagraph(new Reader(List.of(content)), Map.of(), resolver, Map.of()));
if (link instanceof Link l) {
elements.add(l.options().getOrDefault("role", "").contains("inline-code") ?
l :
new Link(l.url(), l.label(),
// inject role inline-code
Stream.concat(
l.options().entrySet().stream().filter(it -> !"role".equals(it.getKey())),
Stream.of(entry("role", (l.options().getOrDefault("role", "") + " inline-code").stripLeading())))
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue))));
}
} else {
elements.add(new Code(content, List.of(), Map.of(), true));
}
i = end;
start = end + 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,11 @@ public void visitLink(final Link element) {
builder.append(" <p>");
}

final boolean code = element.options().getOrDefault("role", "").contains("inline-code");
if (code) {
builder.append("<code>");
}

builder.append(" <a href=\"").append(element.url()).append("\"");
writeCommonAttributes(element.options(), null);

Expand All @@ -380,6 +385,9 @@ public void visitLink(final Link element) {
}
builder.append(">").append(escape(label)).append("</a>\n");

if (code) {
builder.append("</code>");
}
if (parentNeedsP) {
builder.append("</p>");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,14 @@ void links() {
body.children());
}

@Test
void linkInCode() {
final var body = new Parser().parseBody(new Reader(List.of("`https://yupiik.io[Yupiik OSS]`")), null);
assertEquals(
List.of(new Link("https://yupiik.io", "Yupiik OSS", Map.of("role", "inline-code"))),
body.children());
}

@Test
void linkMacroWithRole() {
assertEquals(
Expand Down

0 comments on commit 808d59c

Please sign in to comment.