Insert entity reference with scripts or markdown templates

I am wondering if there is currently a way of inserting entity references in rich text areas via scripts or through markdown templates.

The help page on markdown templates seems to show only inserting some attributes as simple text. I thought that using inline javascript might help but I couldn’t find a way of constructing an inline reference and appending it to the rich text field.

1 Like

A #mention to an entity is constructed in a Fibery-markdown compatible form as follows:
[[#^xxxxxx/yyyyyy]]
where xxxxxx is the ID of the database and yyyyyy is the ID of the entity.

You can use scripting to determine xxxxxx and yyyyyy.

I hope that you already know how to get yyyyyy.

For the database ID, you’ll probably need something like this:

const fibery = context.getService(‘fibery’);
const schema = await fibery.getSchema();
const dbID = schema[‘typeObjects’].filter((obj) => obj.name == ‘spacename/dbname’)[0][‘id’];

I hope this makes sense, and is informative enough to allow you to experiment and achieve what you want to do.

4 Likes

Just in case some else is looking for something similar, here is a how I used markdown to insert a table of outstanding tasks into my “Daily Notes” entries:

# ✅ Tasks
| **Task** | **Due Date** |
| --- | :-: |<%
    const fibery = context.getService('fibery');
    const APP_NAME = "Tasks";
    const ENTITY_TYPE = "Task";
    const SET_TZ ='America/Seattle';

    function getLocalDate(strDate) {
        const dateUTC = new Date(strDate);
        return dateUTC.toLocaleString('en-US', { timeZone: SET_TZ, weekday: 'short', year: 'numeric', month: 'short', day: 'numeric', hour: 'numeric', minute: 'numeric', hour12: true });
    }

    const queryOpenTasks = `{
        findTasks(orderBy: {daysRemaining: ASC}, daysRemaining: {lessOrEquals: 7}, status: {name: {notIn: ["Done"]}}) {
            id
            name
            dueDate
        }
    }`;
    const myOpenTasks = await fibery.graphql('Tasks', queryOpenTasks);

    const schema = await fibery.getSchema();
    const dbID = schema['typeObjects'].filter((obj) => obj.name == `${APP_NAME}/${ENTITY_TYPE}`)[0]['id'];

    myOpenTasks.data.findTasks.forEach((task) => {%>
| [[#^<%= dbID %>/<%= task.id %>]] | <%= getLocalDate(task.dueDate) %> | <% });%>

I must say the new graphql function makes things alot easier (although I am not sure if I’m doing it properly).

Fibery’s markdown table implementation is a bit buggy as it doesn’t respect alignment and table headers.

All of this will be moot once you are able to include live table views as blocks, but thought I should share since @Chr1sG shared the main secret :grin:

2 Likes