opengrok icon indicating copy to clipboard operation
opengrok copied to clipboard

Render all markdown files via showdown

Open JozoVilcek opened this issue 6 years ago • 4 comments

OpenGrok seems to be rendering only markdown file only when listing directories. When I click arbitrary *.md file to see the content, I see source code.

Would it be possible and useful to always render markdown files, for cases like searching / browsing documentation?

JozoVilcek avatar May 20 '19 12:05 JozoVilcek

I don't see why not. For source code content there is the 'Raw' link.

vladak avatar May 20 '19 12:05 vladak

At least one major tech company's internal code search tool does this, and it is great. It allows people to add formatted documentation in the source tree and to leverage the same benefits of source control (pre-checkin review, full change descriptions, the ability to easily see diffs and revision history), and it avoids much of the need for wikis or other out-of-band documentation, which often is less discoverable and is more likely to become stale.

jamesderlin avatar Jul 28 '24 20:07 jamesderlin

Can we see a rendered Markdown file when opening a '*.md' file? I'm currently seeing the code instead. Additionally, only the README.md file is being rendered on the repository page. Can we modify this behavior?

siddharth-ranjan avatar Oct 10 '24 07:10 siddharth-ranjan

OpenGrok seems to be rendering only markdown file only when listing directories. When I click arbitrary *.md file to see the content, I see source code.

Would it be possible and useful to always render markdown files, for cases like searching / browsing documentation?

I don't see why not. For source code content there is the 'Raw' link.

Answer: Yes we can, I did it in client side rendering. If you have done setup like this. You may go to the directory where tomcat is installed, /tomcat/webapps/source. Open list.jsp and search "data-navigate-window-enabled". You will find three match results, go to the second one and replace/comment the div which contains this keyword with

<div id="src" data-navigate-window-enabled="<%= navigateWindowEnabled %>">
    <%
    boolean isMarkdown = xrefFile.getName().contains(".md");
    boolean compressed = xrefFile.getName().endsWith(".gz");

    if (isMarkdown) {
%>
        <div id="markdown-content" style="margin-left: 20px;"></div> <!-- Placeholder for rendered Markdown -->
        <script src="https://cdnjs.cloudflare.com/ajax/libs/marked/14.1.3/marked.min.js"></script>

        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/styles/default.min.css">
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/highlight.min.js"></script>

	<script>
	    (async function renderMarkdown() {
        	const response = await fetch('<%= request.getContextPath() + Prefix.DOWNLOAD_P + Util.uriEncodePath(cfg.getPath()) %>');
        	const markdownText = await response.text();

		// Convert Markdown to HTML
		const renderedHTML = marked.parse(markdownText);
	        document.getElementById('markdown-content').innerHTML = renderedHTML;

		// Apply syntax highlighting
		document.querySelectorAll('pre code').forEach((block) => {
		    hljs.highlightElement(block);
		});
	    })();
	</script>


<%
    } else {
%>
        <pre><%
            Util.dumpXref(out, xrefFile, compressed, request.getContextPath());
        %></pre>
<%
    }
%>

</div>

edit You can host the cdns locally for better performance and security.

Thanks.

siddharth-ranjan avatar Oct 28 '24 09:10 siddharth-ranjan