Skip to content

Commit

Permalink
Generate ID to headerless Markdown
Browse files Browse the repository at this point in the history
Signed-off-by: Jarno Elovirta <jarno@elovirta.com>
  • Loading branch information
jelovirt committed May 11, 2024
1 parent 98ee818 commit 6d77802
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
17 changes: 13 additions & 4 deletions src/main/java/com/elovirta/dita/markdown/MarkdownParserImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ protected Document preprocess(Document root, URI input) throws SAXException {
new SAXParseException(MESSAGES.getString("error.missing_title"), null, input.toString(), 1, 1)
);
}
generateRootHeading(root, null);
generateRootHeading(root, getTextFromFile(input).toLowerCase().trim().replaceAll("[\\s_]+", "-"), null);
}
}
return root;
Expand All @@ -166,15 +166,15 @@ private void generateRootHeading(Document root, URI input) {
final YamlFrontMatterBlock yaml = root.getFirstChild() instanceof YamlFrontMatterBlock
? (YamlFrontMatterBlock) root.getFirstChild()
: null;
String title = input != null ? getTextFromFile(input) : null;
final Heading heading = new Heading();
String title = getTextFromFile(input);
String id = null;
if (yaml != null) {
final AbstractYamlFrontMatterVisitor v = new AbstractYamlFrontMatterVisitor();
v.visit(root);
final Map<String, List<String>> metadata = v.getData();
final List<String> ids = metadata.get("id");
if (ids != null && !ids.isEmpty()) {
heading.setAnchorRefId(ids.get(0));
id = ids.get(0);
}
final List<String> titles = metadata.get("title");
if (titles != null && !titles.isEmpty()) {
Expand All @@ -187,6 +187,15 @@ private void generateRootHeading(Document root, URI input) {
}
}
}

generateRootHeading(root, id, title);
}

private void generateRootHeading(Document root, String id, String title) {
final Heading heading = new Heading();
if (id != null) {
heading.setAnchorRefId(id);
}
heading.setLevel(1);
if (title != null) {
final AnchorLink anchorLink = new AnchorLink();
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/dita/missing_root_header.dita
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<topic xmlns:ditaarch="http://dita.oasis-open.org/architecture/2005/" class="- topic/topic "
specializations="@props/audience @props/deliveryTarget @props/otherprops @props/platform @props/product"
id="" ditaarch:DITAArchVersion="2.0">
id="missing-root-header" ditaarch:DITAArchVersion="2.0">
<title class="- topic/title "></title>
<body class="- topic/body ">
<p class="- topic/p ">Root topic content.</p>
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/dita/missing_root_header_with_yaml.dita
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<topic xmlns:ditaarch="http://dita.oasis-open.org/architecture/2005/" class="- topic/topic "
specializations="@props/audience @props/deliveryTarget @props/otherprops @props/platform @props/product"
id="" ditaarch:DITAArchVersion="2.0">
id="missing-root-header-with-yaml" ditaarch:DITAArchVersion="2.0">
<title class="- topic/title "></title>
<body class="- topic/body ">
<p class="- topic/p ">Root topic content.</p>
Expand Down

0 comments on commit 6d77802

Please sign in to comment.