NotePlan / NotePlan/plugins

Templating: Make docs clear about two dashes

オープン
#428 コメント 0 件 リアクション 0 件 担当者 1 名 GitHub で見る

@mikeerickson がすでに取り組んでいます。

2023年3月22日 から。

documentation
主要言語
JavaScript
スター
204
フォーク
82
平均マージ
22時間 27分
マージ済み PR(30日)
3

説明

  1. Pls merge the PR on the docs from @ed when you can
  2. When you do, pls make one addition: make it super explicitly clear (maybe in multiple places) that if you want a frontmatter in your template, you should use two dashes "--" which will get converted to three when the template is rendered. Technically, you can now put three dash separators in a template, but NOT as the first line. So, if you want the top of your template to be frontmatter in the generated note, then use two dashes. You should NOT ever have three dashes as the first line in the body of your template.

I spent a couple hours chasing my tail trying to find a bug a user had and it turned out the problem was that three dashes in the first line of the template BODY is a no-no and Templating will try to render it as if it's frontmatter. I'm not sure why exactly this happens (maybe reentrance?) but preRender is called more than once when rendering it.

@mikeerickson why does it call preRender twice?
For example, templateAppend does this:

      let { frontmatterBody, frontmatterAttributes } = await NPTemplating.preRender(templateData)
      let data = { ...frontmatterAttributes, frontmatter: { ...frontmatterAttributes } }

      let renderedTemplate = await NPTemplating.render(frontmatterBody, data)

It calls preRender and then afterwards calls render with the results. The problem is that render() calls preRender again:

        const { frontmatterAttributes, frontmatterBody } = await this.preRender(templateData, sessionData)

And it's this second time through that causes the issues if you have dashes in the first line.

Fixing it feels low priority if the documentation can be super clear that you cannot ever place three dashes as the first line of your template body. For example (note that the first line UNDER the frontmatter is three dashes):

---
title: this template will fail to render
---
---
<%- date.now("Do MMMM YYYY") %>
---
foo bar

I created a test case to illustrate the issue, but it is skipped for now because obviously it will fail:

    //FIXME: (@codedungeon): - I added this test to illustrate an edge case that a user was running into
    // Even though the above test on .render passes using Jest, in the real NotePlan app,
    // if the templateBody starts with three dashes, then for some reason, preRender gets called on that body as if it's frontmatter and fails
    // in the same way it fails in this test
    it.skip(`should preRender with multiple frontmatter-like separators in document (even number) - esp when the first line in the template content is a separator`, async () => {
      const templateData = `---\n<%- date.now("Do MMMM YYYY") %>\n\n---`
      const sessionData = {
        title: 'Daily Note Test',
        type: 'meeting-note, empty-note',
        methods: {},
      }
      let result = await NPTemplating.preRender(templateData, sessionData)
      const lines = result.split('\n')
      expect(lines[0]).toEqual(`---`)
      // date on line 1
      expect(lines[2]).toEqual(``) //empty line
      expect(lines[3]).toEqual(`## Primary Focus`)
    })
    ```


コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。