gomarkdown / markdown

markdown parser and HTML renderer for Go
Other
1.36k stars 171 forks source link

Set title automatically #271

Closed benstigsen closed 1 year ago

benstigsen commented 1 year ago

If I had the following:

# Hello There
Some content

When html.CompletePage is set, would it then be possible to generate <title>Hello There</title> or to add an option like TitleFromFirstH1, to set the title?

kjk commented 1 year ago

That seems too specific of a use case. You can traverse AST tree of parsed markdown to find first heading or customize html renderer to override header rendering function to remember the title and after rendering is done, append in front of generated html.</p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/igorcafe"><img src="https://avatars.githubusercontent.com/u/85039990?v=4" />igorcafe</a> commented <strong> 1 year ago</strong> </div> <div class="markdown-body"> <p>@kjk Could you give an example of how to do that? I'm following the docs but I'm really confused with "AsContainer", "AsLeaf", and I'm not able to get their contents or their types...</p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/igorcafe"><img src="https://avatars.githubusercontent.com/u/85039990?v=4" />igorcafe</a> commented <strong> 1 year ago</strong> </div> <div class="markdown-body"> <p>And it doesn't seem to have a <code>innerText</code> or something like that</p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/igorcafe"><img src="https://avatars.githubusercontent.com/u/85039990?v=4" />igorcafe</a> commented <strong> 1 year ago</strong> </div> <div class="markdown-body"> <p>@BenStigsen @kjk I managed to do it :) In my case the h1 will always be pure text, so I made:</p> <pre><code class="language-go">ast.WalkFunc(doc, func(n ast.Node, entering bool) ast.WalkStatus { if !entering { return ast.Terminate } h, ok := n.(*ast.Heading) if !ok { return ast.GoToNext } if h.Level != 1 { return ast.GoToNext } t, ok := ast.GetFirstChild(h).(*ast.Text) if !ok { title = string(t.Literal) } return ast.Terminate })</code></pre> <p>It may have a quickier way, but that is how I managed to do...</p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/semanser"><img src="https://avatars.githubusercontent.com/u/4020045?v=4" />semanser</a> commented <strong> 6 months ago</strong> </div> <div class="markdown-body"> <p>@igoracmelo your solution works, thanks! But I had to modify it a bit. Instead of </p> <pre><code>t, ok := ast.GetFirstChild(h).(*ast.Text) if !ok { title = string(t.Literal) }</code></pre> <p>It should be</p> <pre><code>t, ok := ast.GetFirstChild(h).(*ast.Text) if ok { // notice the !ok -> ok change title = string(t.Literal) }</code></pre> <p>Hope it will help someone who'll arrive here from search :)</p> </div> </div> <div class="page-bar-simple"> </div> <div class="footer"> <ul class="body"> <li>© <script> document.write(new Date().getFullYear()) </script> Githubissues.</li> <li>Githubissues is a development platform for aggregating issues.</li> </ul> </div> <script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script> <script src="/githubissues/assets/js.js"></script> <script src="/githubissues/assets/markdown.js"></script> <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/highlight.min.js"></script> <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/go.min.js"></script> <script> hljs.highlightAll(); </script> </body> </html>