OpenZeppelin / solidity-docgen

Documentation generator for Solidity projects
MIT License
452 stars 118 forks source link

Using handlebars inside the file headers does not work #442

Closed migoldfinger closed 1 year ago

migoldfinger commented 1 year ago

I create all my markdown files with a header like

---
filename: {{ site.baseurl }}/contracts/Lock
slug: Lock
type: contract
---
Just a Test {{ site.baseurl }}

The usage of {{ site.baseurl }} inside the header gives me an error (): did not find expected key while parsing a block mapping at line 2 column 1

The second usage outside of the header works just fine. So how can I get the value of baseurl inside the header?

I build the header with a function in a helpers.ts class but did not find a way to access the value so I thought I could use the handlebar. My workaround would be to hardcode the baseurl into my helpers.ts class but I do not like it.

frangio commented 1 year ago

Can you show the function you wrote in helpers.ts?

migoldfinger commented 1 year ago
export function fileHeader(this: DocItemWithContext)
{
    if (this.nodeType === "ContractDefinition")
    {
        const dic = this[DOC_ITEM_CONTEXT];
        if (!dic.page) return "**helpers.fileHeader:page property must be present!**";
        const purePath = extractDocPath(dic);

        let ret = "---\n";
        ret += `filename: ${purePath}\n`;
        ret += `slug: ${dic.item.canonicalName}\n`;
        ret += `type: ${dic.item.contractKind}\n`;
        ret += "---";
        return ret;
    }
}

function extractDocPath(dic: DocItemContext): string
{
    const regex = /\.md$/;
    if (!dic.page) return "/index";
    let result = dic.page.replace(regex, "");
    if (result[0] !== "/")
    {
        result = "/" + result;
    }
    return "/my-contracts" + result;
}
frangio commented 1 year ago

If you output a Handlebars tag from a helper it will not be rendered.

I'm actually not sure where baseurl is coming from, so I don't know if this will help you, but you can access the global site variable in the helper like this:

export function fileHeader(this: DocItemWithContext, opts: any) {
  const site = opts.data.site;
  ...
}
migoldfinger commented 1 year ago

@frangio Not so fast. opts.data.site holds data regarding the contract. baseurl is a variable taken from the _config.yml file in the doc root folder defining the documentation build properties. I did not see a way to access this value inside the helper.ts

frangio commented 1 year ago

@migoldfinger Sorry, I'm not sure what _config.yml is but it is not handled by solidity-docgen in any way.