ipfs-inactive / examples

[ARCHIVED] DEPRECATED — merged into IPFS docs
185 stars 74 forks source link

Markdown-Viewer (mdown) document title #36

Open jbshirk opened 9 years ago

jbshirk commented 9 years ago

Using the latest version of mdown, I demonstrated to a potential client the elegant usefulness of this app for distributing content easily generated by the non-technical person. Client remarked (and I agree) that the browser tab <title> is hard-coded for all content passed to the app via #hash, and asked if there is a way to make the document title relevant to the specific content displayed. I tried an experiment, a modification, that assumes the first <h1> or <h2> that is generated from the markdown content is intended to be the doc title, and attempted to set the doc tile = firstheadingtext. I was not able to get it to work.

In a simple one-page.html document (not mdown) I confirmed that the following works:

 document.title = document.querySelector("h1, h2").textContent;

But in mdown it is not that simple. I was able to set the title to a dummy at the end of all scripts to verify that changing the location#hash does set the title. But the above code is out of the correct context and spins forever, never finding my <h2> generated from the .md file, and the debugger shows it returns NULL (not an object) so that in fact the page never finishes loading.

I was wondering if someone could give me some pointers on how to access the rendered DOM from within mdown to return the first heading's text. Better ideas are welcome. I am not a js person, if that explains my apparent cluelessness. Thanks!

victorb commented 9 years ago

The example you are showing (document.title = document.querySelector("h1, h2").textContent) should be working, I confirmed it with the javascript console when being here: https://ipfs.io/ipfs/QmSrCRJmzE4zE1nAfWPbzVfanKQNBhp7ZWmMnEdbiLvYNh/mdown#/ipfs/QmfQ75DjAxYzxMP2hdm6o4wFwZS5t7uorEZ2pX9AKXEg2u

The first thing I would try is to place you snipped after the content actually been loaded, and the loading have been hidden, which would be after this line: https://github.com/ipfs/examples/blob/master/webapps/markdown-viewer/mdown#L49

Otherwise, please show the code you're trying and maybe we can spot where things go wrong :)

jbshirk commented 9 years ago

That worked! Thank you so much!!

jbenet commented 9 years ago

could you please PR the changes? am sure everyone wants this

jbshirk commented 9 years ago

Unfortunately, I am not yet that savy with git. I cloned it to my local, and simply added the line where indicated. Check this: https://ipfs.io/ipfs/Qmbm4DRJd5eCMNeeHrtrJ4vYJGYAkbu2ZXtisb6C5ciWY1/md#uphold-holdup

I don't think people will want my changes, because I didn't want to use 'mdown' and changed it to 'md' as well as the reference to mdown in sample.md

jbshirk commented 9 years ago

Anyway, it is still suboptimal because the metadata scraped when posting to a site shows "markdown reader" as the title, which is hard-coded in the mdown html. I'd be interested in suggestions if this is of interest. Otherwise you can close it again if you want.