Skip to content

Commit 8a0c1f5

Browse files
committed
SOLR-15092: eliminate overly strict rules against empty link anchors in ref-guide
legacy enforcement from the days of building a PDF
1 parent d95e405 commit 8a0c1f5

File tree

1 file changed

+15
-22
lines changed

1 file changed

+15
-22
lines changed

solr/solr-ref-guide/tools/CheckLinksAndAnchors.java

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
* Validates that no file contains the same anchor more then once.
5151
* </li>
5252
* <li>
53-
* Validates that relative links point to a file that actually exists, and if it's part of the ref-guide that the '#fragement' in the link refers to an ID that exists in that file.
53+
* Validates that relative links point to a file that actually exists, and if it's part of the ref-guide that any '#fragment' in the link refers to an ID that exists in that file.
5454
* </li>
5555
* <li>
5656
* Our use of "<a href="https://getbootstrap.com/">Bootstrap</a>" features leverage some custom javascript
@@ -182,7 +182,7 @@ public static void main(String[] args) throws Exception {
182182
nodesWithIds.add(new Element(Tag.valueOf("body"), "").attr("id", file.getName().replaceAll("\\.html$","")));
183183
} else {
184184
// We have to add Jekyll's <body> to the nodesWithIds so we check the main section anchor as well
185-
// since we've already
185+
// since we've already drilled down below it
186186
nodesWithIds.addAll(doc.select("body[id]"));
187187
}
188188

@@ -214,7 +214,7 @@ public static void main(String[] args) throws Exception {
214214
totalIds++; // Note: we specifically don't count 'preamble'
215215
}
216216

217-
// check for (relative) links that don't include a fragment
217+
// build up the list of (relative) linksInThisFile
218218
final Elements links = mainContent.select("a[href]");
219219
for (Element link : links) {
220220
totalLinks++;
@@ -227,17 +227,8 @@ public static void main(String[] args) throws Exception {
227227
final URI uri = new URI(href);
228228
if (! uri.isAbsolute()) {
229229
totalRelativeLinks++;
230-
final String frag = uri.getFragment();
231-
if ((null == frag || "".equals(frag)) && ! uri.getPath().startsWith("../")) {
232-
// we must have a fragment for intra-page links to work correctly
233-
// but relative links "up and out" of ref-guide (Ex: local javadocs)
234-
// don't require them (even if checkAllRelativeLinks is set)
235-
problems++;
236-
System.err.println(file.toURI().toString() + " contains relative link w/o an '#anchor': " + href);
237-
} else {
238-
// track the link to validate it exists in the target doc
239-
linksInThisFile.add(uri);
240-
}
230+
// track the link to (later) validate the target doc exists and contains the linked anchor (if any)
231+
linksInThisFile.add(uri);
241232
}
242233
} catch (URISyntaxException uri_ex) {
243234
// before reporting a problem, see if it can be parsed as a valid (absolute) URL
@@ -260,7 +251,7 @@ public static void main(String[] args) throws Exception {
260251
problems += validateHtmlStructure(file, mainContent);
261252
}
262253

263-
// check every (realtive) link in every file to ensure the frag exists in the target page
254+
// check every (relative) link in every file to ensure the target page exists, and contains the linked anchor (if any)
264255
for (Map.Entry<File,List<URI>> entry : filesToRelativeLinks.entrySet()) {
265256
final File source = entry.getKey();
266257
for (URI link : entry.getValue()) {
@@ -276,14 +267,16 @@ public static void main(String[] args) throws Exception {
276267
} else {
277268
if ( ! path.startsWith("../") ) {
278269
// if the dest file is part of the ref guide (ie: not an "up and out" link to javadocs)
279-
// then we validate the fragment is known and exists in that file...
270+
// then we validate the fragment (if any) is known and exists in that file...
280271
final String frag = link.getFragment();
281-
final Set<String> knownIdsInDest = filesToIds.get(dest.getName());
282-
assert null != knownIdsInDest : dest.getName();
283-
if (! knownIdsInDest.contains(frag) ) {
284-
problems++;
285-
System.err.println("Relative link points at id that doesn't exist in dest: " + link);
286-
System.err.println(" ... source: " + source.toURI().toString());
272+
if ( ! (null == frag || frag.isEmpty()) ) {
273+
final Set<String> knownIdsInDest = filesToIds.get(dest.getName());
274+
assert null != knownIdsInDest : dest.getName();
275+
if (! knownIdsInDest.contains(frag) ) {
276+
problems++;
277+
System.err.println("Relative link points at id that doesn't exist in dest: " + link);
278+
System.err.println(" ... source: " + source.toURI().toString());
279+
}
287280
}
288281
}
289282
}

0 commit comments

Comments
 (0)