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

await is only valid in async functions #7

Open
sheep94lion opened this issue Nov 3, 2021 · 0 comments
Open

await is only valid in async functions #7

sheep94lion opened this issue Nov 3, 2021 · 0 comments

Comments

@sheep94lion
Copy link

The customRequestHandler return error: await is only valid in async function.
Removing await keyword from the code seems working.
trilium version: 0.48.1-beta
My revised code:

// HowTo:
//  1- Add this file to trilium as a `JS Backend` code note.
//  2- Set label `customRequestHandler` to 'singlefile2trilium'.

const template = `
<script>
    var iframe = document.getElementById('r-iframe');

    function pageY(elem) {
        return elem.offsetParent ? (elem.offsetTop + pageY(elem.offsetParent)) : elem.offsetTop;
    }

    function resizeIframe() {
        var height = document.documentElement.clientHeight;
        height -= pageY(iframe) + 20 ;
        height = (height < 0) ? 0 : height;
        iframe.style.height = height + 'px';
    }

    iframe.onload = resizeIframe;
    window.onresize = resizeIframe;
</script>

<iframe id="r-iframe" style="width:100%" src="data:text/html;charset=utf-8,%%CONTENT%%" sandbox=""></iframe>
`;

const {req, res} = api;
const {title, url, content} = req.body;

if (req.method == 'POST') {
    api.log("==========================");
    // create render note
    const renderNote = (api.createNewNote({
        parentNoteId: "IIgUiHfe3hu0",
        title: title,
        content: '',
        type: 'render'
    })).note;
    renderNote.setLabel('clipType', 'singlefile2trilium');
    renderNote.setLabel('pageUrl', url);
    renderNote.setLabel('pageTitle', title);

    // create child `content.html`
    var wrapped_content = template.replace("%%CONTENT%%", encodeURIComponent(content));
    const htmlNote = (api.createNewNote({
        parentNoteId: renderNote.noteId,
        title: 'content.html',
        content: wrapped_content,
        type: 'code',
        mime: 'text/html'
    })).note;
    htmlNote.setLabel('archived');

    // link renderNote to htmlNote
    renderNote.setRelation('renderNote', htmlNote.noteId);

    res.send(201); // http 201: created
}
else {
    res.send(400); // http 400: bad request
}

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

No branches or pull requests

1 participant