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

Support and display YAML front-matter block contents #1597

Merged
merged 46 commits into from
Feb 20, 2022

Conversation

fgtham
Copy link
Contributor

@fgtham fgtham commented Feb 13, 2022

Most of my documents contain a leading YAML front-matter block with title, creation date and tags attributes. I'd really like that information to be displayed on top of the note in view mode. This PR adds support for it. It must be switched on in Settings / Markdown / Show YAML-Block.

These points apply:

  • keys must consist of alpha-numeric characters, no spaces, punctuation, etc.
  • attribute values are either strings or lists
  • the tags attribute is special: if it's a string of comma-separated values, it is split into a real list, so these two tags are equivalent:
    tags:
      - tag1
      - tag2
    # equivalent to:
    tags: tag1, tag2
  • single or double quotes surrounding a string are removed

Assume this block:

---
foo: "bar baz"
tags: tag1, tag2, tag3
alist:
  - an item
  - another item
---

The resulting HTML is:

<div class='yaml-front-matter-container'>
  <div class='yaml-front-matter-item yaml-foo-container'>
    <span class='yaml-foo-item'>
      bar baz
    </span>
  </div>
  <div class='yaml-front-matter-item yaml-tags-container'>
    <span class='yaml-tags-item'>
      tag1
    </span>
    <span class='yaml-tags-item'>
      tag2
    </span>
    <span class='yaml-tags-item'>
      tag3
    </span>
  </div>
  <div class='yaml-front-matter-item yaml-alist-container'>
    <span class='yaml-alist-item'>
      an item
    </span>
    <span class='yaml-alist-item'>
      another item
    </span>
  </div>
</div>

The following block:

---
title: Markor Markdown Reference
date: 10.02.2022 06:17:35
title: markdown, reference
---

Will be displayed like that:

Screenshot_20220213-222258

Florian Tham added 3 commits February 13, 2022 22:15
Must be switched on in Settings / Markdown / Show YAML-Block.

These points apply:
* keys must consist of alpha-numeric characters, no spaces, punctuation,
  etc.
* attribute values are either strings or lists
* the `tags attribute` is special: if it's a string of comma-separated
  values, it is split into a real list
* single or double quotes surrounding a string are removed

The following two `tags` values are equivalent:

```
---
tags:
  - tag1
  - tag2
tags: tag1, tag2
---
```

Assume this block:

```
---
foo: bar
tags: tag1, tag2, tag3
alist:
  - an item
  - another item
---
```

The resulting HTML is:

```
<div class='yaml-front-matter-container'>
  <div class='yaml-front-matter yaml-foo-container'>
    <span class='yaml-foo-item'>
      bar
    </span>
  </div>
  <div class='yaml-front-matter yaml-tags-container'>
    <span class='yaml-tags-item'>
      tag1
    </span>
    <span class='yaml-tags-item'>
      tag2
    </span>
    <span class='yaml-tags-item'>
      tag3
    </span>
  </div>
  <div class='yaml-front-matter yaml-alist-container'>
    <span class='yaml-alist-item'>
      an item
    </span>
    <span class='yaml-alist-item'>
      another item
    </span>
  </div>
</div>
```
Specify which attributes to show in Settings / Markdown / YAML Block:

* '': don't show any attributes
* '*': show all attributes
* 'attr1, attr2, ...': only show selected attributes
Copy link
Owner

@gsantner gsantner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My suggestion is to get rid of the + v + part that adds the content.

I suggest instead to place a token there like {{ post.<<key>> }}, which is later replaced by the value.

I did quite a similar thing in another project .... generate the proper html escaped strings, and replace post.key, page.key & site.key with that value.

This means automatically, in case the user has somewhere {{ page.title }} in the file, it gets the frontmatter title 😄

grafik

yamlFrontMatterMap = extractYamlFrontMatter(markup);

if (!yamlFrontMatterMap.isEmpty()) {
for (Map.Entry<String, List<String>> entry : yamlFrontMatterMap.entrySet()) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

watch out for Zim Wiki (as it's transpiled to Markdown), do the files work fine?

maybe some additional unit-test(s) that compares input and output could be helpful generally

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I wasn't aware of Zim Wiki. I will have a look.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a look at some Zim files but could not find any issues. Do you have anything in mind which could lead to problems?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no. i dont use zim either.

@fredericjacob

Florian Tham added 5 commits February 15, 2022 09:45
Instead of switching YAML block display on and off, allow the user to
define which attributes she wants to see by editing the setting in
Settings / Markdown / Show YAML-Block. Allowed values are:

* '' (empty string): turn off, i.e. nothing is displayed
* '*' (star): display all attributes
* 'attr1, attr2, ...': comma-separated list of attributes to show
@fgtham
Copy link
Contributor Author

fgtham commented Feb 15, 2022

I like the idea of using tokens instead, will see if I get it working.

In fact, the whole range of Markdown works in the attributes:

```yaml
---
title: Foo `bar` $a+b$
---
```
@gsantner gsantner force-pushed the master branch 3 times, most recently from a818d5b to 757efd4 Compare February 15, 2022 18:58
This reverts commit e124bb4.

Performance issue
@gsantner
Copy link
Owner

gsantner commented Feb 16, 2022

should be something like that -->

for (String k : fronmattermap) {
   final String v = TextUtils.htmlEncode(frontmattermap.at(k).join(", "));
   for (String scope : "page,post,site".split(",")) {
       content = content.replace("{{ " + scope + "." + k + " }}", v);
   }
}

for tags and date it can optionally be special handling, for the rest this is fine.

My goal is that your new settings option is mainly about adding your newly added predefined Heading. This is then similar to how TOC table of contents is done.

@gsantner
Copy link
Owner

gsantner commented Feb 16, 2022

let me know when it's reviewable again

@fgtham
Copy link
Contributor Author

fgtham commented Feb 20, 2022

Does it make sense to have three scopes 'page', 'post' and 'site'? If we drop two and only use 'post', the code will be even simpler. No need to iterate over the scope list for every attribute any longer, cutting down the mumber of replace() calls.

@gsantner
Copy link
Owner

gsantner commented Feb 20, 2022

I'm OK with it if it is just post. Though I would keep the code as is, but just remove site & page from the array. So a loop with just one entry then.


I took care of it, you don't need to make change for this.

@gsantner
Copy link
Owner

do you have something left, otherwise I merge it now

@fgtham
Copy link
Contributor Author

fgtham commented Feb 20, 2022

Thanks! I have nothing left, have been using it for a while with no apparent issues.

@fgtham
Copy link
Contributor Author

fgtham commented Feb 20, 2022

In app/src/main/res/values/strings.xml line 261 shouldn't this read 'post.title' instead of 'page.title'?

@gsantner
Copy link
Owner

now yes, before reducing to just post .. it would have worked : - )

@gsantner gsantner added this to the 2.9 milestone Feb 20, 2022
gsantner
gsantner previously approved these changes Feb 20, 2022
@gsantner gsantner merged commit dac68bf into gsantner:master Feb 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants