Don't render navbar.md metadata in html
After using a named link in navbar.md as metadata to activate extensions
they remained rendered in html as plain text
Change-Id: I5562853692f763f58be08e132be1ce550aafd9dd
diff --git a/java/com/google/gitiles/doc/Navbar.java b/java/com/google/gitiles/doc/Navbar.java
index c05f9d9..c866aa9 100644
--- a/java/com/google/gitiles/doc/Navbar.java
+++ b/java/com/google/gitiles/doc/Navbar.java
@@ -89,8 +89,8 @@
}
private void parse(String markdown) {
- extractMetadata(markdown);
- node = GitilesMarkdown.parse(cfg, markdown);
+ String md = extractMetadata(markdown);
+ node = GitilesMarkdown.parse(cfg, md);
extractSiteTitle();
}
@@ -107,24 +107,33 @@
}
}
- private void extractMetadata(String markdown) {
+ private String extractMetadata(String markdown) {
Matcher m = META_LINK.matcher(markdown);
- while (m.find()) {
- String key = m.group(1).toLowerCase();
- String url = m.group(2).trim();
- switch (key) {
- case "logo":
- logoUrl = url;
- break;
- case "home":
- homeUrl = url;
- break;
- case "extensions":
- Set<String> names = splitExtensionNames(url);
- cfg = cfg.copyWithExtensions(enabled(names), disabled(names));
- break;
- }
+ boolean foundMetadata = m.find();
+ if (foundMetadata) {
+ StringBuffer sb = new StringBuffer();
+ do {
+ String key = m.group(1).toLowerCase();
+ String url = m.group(2).trim();
+ switch (key) {
+ case "logo":
+ logoUrl = url;
+ break;
+ case "home":
+ homeUrl = url;
+ break;
+ case "extensions":
+ Set<String> names = splitExtensionNames(url);
+ cfg = cfg.copyWithExtensions(enabled(names), disabled(names));
+ break;
+ }
+ m.appendReplacement(sb, "");
+ foundMetadata = m.find();
+ } while (foundMetadata);
+ m.appendTail(sb);
+ return sb.toString();
}
+ return markdown;
}
private static Set<String> splitExtensionNames(String url) {
diff --git a/javatests/com/google/gitiles/doc/DocServletTest.java b/javatests/com/google/gitiles/doc/DocServletTest.java
index 5d47cd8..48300fc 100644
--- a/javatests/com/google/gitiles/doc/DocServletTest.java
+++ b/javatests/com/google/gitiles/doc/DocServletTest.java
@@ -40,7 +40,12 @@
@Test
public void includesNavbar() throws Exception {
- String navbar = "# Site Title\n" + "\n" + "* [Home](index.md)\n" + "* [README](README.md)\n";
+ String navbar =
+ "# Site Title\n"
+ + "\n"
+ + "* [Home](index.md)\n"
+ + "* [README](README.md)\n"
+ + "[extensions]: blocknote\n";
repo.branch("master")
.commit()
.add("README.md", "# page\n\nof information.")
@@ -53,6 +58,7 @@
assertThat(html).contains("<span class=\"Header-anchorTitle\">Site Title</span>");
assertThat(html).contains("<li><a href=\"/b/repo/+/master/index.md\">Home</a></li>");
assertThat(html).contains("<li><a href=\"/b/repo/+/master/README.md\">README</a></li>");
+ assertThat(html).doesNotContain("extensions");
assertThat(html)
.contains("<h1><a class=\"h\" name=\"page\" href=\"#page\"><span></span></a>page</h1>");
}