Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to access metadata #9

Closed
ponychicken opened this issue Oct 26, 2016 · 8 comments
Closed

How to access metadata #9

ponychicken opened this issue Oct 26, 2016 · 8 comments

Comments

@ponychicken
Copy link

ponychicken commented Oct 26, 2016

According to your docs Writer can process MultiMarkdown metadata. How do I access it inside of a template?

@antons
Copy link
Collaborator

antons commented Oct 27, 2016

Currently, the processing option applies to MultiMarkdown, not iA Writer. Metadata is processed inside data-document, but it can’t be accessed by static template files. We would like to improve this in the future.

@antons antons closed this as completed Oct 27, 2016
@mb21
Copy link

mb21 commented Oct 27, 2016

But can we access it through JavaScript inside the template?

btw, it seems this issue is a duplicate of #6
It would be super handy if you closed issues only if:

  • it's a duplicate
  • once the bug is fixed in the newest shipping version of iA Writer
  • or if the feature request was declined

@antons
Copy link
Collaborator

antons commented Oct 27, 2016

@mb21 That is a good idea. Thank you!

@ponychicken
Copy link
Author

@mb21 could you expand on this javascript idea?

@antons
Copy link
Collaborator

antons commented Oct 27, 2016

@mb21 It’s possible to hack it in by extracting metadata from data-document on ia-writer-change, but it’s not trivial.

@thespacedoctor
Copy link

@antons Can you give us a little more direction with extracting metadata from data-document on ia-writer-change? I would like to get at my MMD metadata ;)

@marcammann
Copy link

Not @antons but what I did was create divs at the top of my document with the interpolated value of the metadata and then accessed the DOM element after the ia-writer-change event.

You'll also have to hide the elements in your styles:

Title: Test

<div class="meta" id="title">[%Title]</div>
<div class="meta" id="brand">[%Brand]</div>

document.getElementById('title').innerText === "Test"

@thespacedoctor
Copy link

thespacedoctor commented Feb 6, 2019

Thanks @marcammann here's what I ended up doing ...

Let's say I have this metadata at the top of my MMD document:

title: my lovely horse  
author: Neil Hannon  
tags: #horse #song  

NOTE STRICT DOUBLE SPACE AT END OF METADATA LINES

Here's what I did in the end to access hashtags from the metadata.

  1. Turn off 'Process Metadata' in preferences so metadata gets rendered within paragraph elements at the top of the body:

  2. Parse the hashtags within javascript. I was only trying to access the tags, but you can use this method to parse out whatever MMD metadata you want:

    // EXTRACT HASH-TAGS AND PRESENT THEM IN HTML RENDERING
    var processHashTags = function() {
        
        // GET HASH TAGS
        tagSpans = document.getElementsByClassName('hashtag')
        tags = []
        for (var i = 0; i < tagSpans.length; i++) {
            tags.push(tagSpans[i].innerHTML.replace("#", ""));
        }
    
        // METADATA IS IN PARAGRAPHS BEFORE FIRST HEADER
        var metadict = {}
        var c = document.body.childNodes;
        for (var i = 0; i < c.length; i++) {
            if (c[i].nodeName == "H1") {
                break
            }
            if (c[i].nodeName == "P" && c[i].innerHTML.includes(":")) {
                var dataelements = c[i].innerHTML.split("<br>")
                for (var j = 0; j < dataelements.length; j++) {
                    keyvalue = dataelements[j].trim().split(":")
                    metadict[keyvalue[0].trim()]=keyvalue[1].trim()
                }
            }
            c[i].remove();
        }
    
        // ADD LINKED HASH TAGS
        var tagsContainer = document.createElement('div')
        tagsContainer.setAttribute('class', 'tags')
        for (var i = 0; i < tags.length; i++) {
            tagsContainer.innerHTML += '<a href="ia-writer://quick-search?query=%23' + tags[i] + '" class="hashtag">#' + tags[i] + '</a>  '
        }
    
        return [tags, tagsContainer, metadict]
    }

    Note that the tagsContainer element here contains your hashtags that are linked to an iA search for those specific tags (super handy in rendered docs).

  3. Finally call processHashTags to access all your metadata (e.g. metadata["title"]) :

    // WAIT FOR THE 'ia-writer-change' EVENT BEFORE ACTING
    document.body.addEventListener('ia-writer-change', function() {
        var dataArray = processHashTags();
        var metadata = dataArray[2];
    })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants