Modular CustomJS and CSS, hidden but published directories and files

Hi All,

TLDR: How do you organize your CSS and JavaScript so it’s available for all your files? How do you syntactically call your custom CSS without needing to write a bunch of HTML (per page)? Is there an idea of “components”? Or can you create one? The CSS has a snippets folder that’s in the .obsidian hidden directory. Can we create other hidden directories? Can we put our JS in the same spot?

I’m getting way more deep into modding Obsidian, and my main vault is actually an e-book for a “client” (pays me). I need a lot of custom CSS and JavaScript (and HTML too). I’m used to building component-based web applications, which generally have conventions on how to organize files. And, when you push the app, your files don’t show up in a menu on the side :wink:

How to best organize this so I have a little framework?

  1. CSS Modules, Scopes
  2. CustomJS, modules (don’t publish as a visible file in the file navigator left pane, but publishes as in available for use)
  3. HTML & JS reusable components with inner-embed data from files.

I’ve already gone through Hide folders from left nav, obsidian-custom-js, Develop and execute modular JavaScript

But they are missing examples and details, so it’s hard to follow for n00bs.

Thanks!

Michael

Also, does anyone know how to save “private” keys so they are available in your JavaScript but won’t be published or otherwise hackable? AI API Keys, ImageKit, OpenGraph, etc etc.

Can you gives us a bit more background about your “e-book” project? What do you want to achieve?

If you’re building a website with Obsidian Publish, have you checked Obsidian’s help page “Customize your site”?

@harr I have spent hours and hours and days and days trying to figure out how to do this. I have read all the documentation. I have read all the previous responses. I have examined the codebases of multiple plugins. I have installed and uninstalled and reinstalled CustomJS, JSEngine, etc.

I will explain in detail below, but to get to the point:

How do you call a JavaScript function from within the Obsidian desktop app, in a specific Markdown file? How do you do it in such a way that it renders both in Edit and in Read mode, as well as when it goes to Publish?

— Longer Explanation —
I am trying to create a beautiful link card that dynamically fetches OpenGraph assets from the links I am linking to. For reference, CraftDocs does this and here’s my example published page of links to Database Apps.

I know how to do this in CSS, HTML, and JavaScript. I have it working on my local machine. How do I then put it into my Obsidian vault, and it renders in Edit, Read, and Publish?

To break down the question:
I know how and where to edit CSS in both the publish.css file and the .obsidian/snippets folder. But I don’t know know how to make the CSS apply to the Obsidian extended markdown (or Markdown in general).

I know how to write JavaScript functions that return an object, but I don’t know how to render it in the Obsidian editor.

I have written a function called fetchOpenGraphData that returns an object with properties.

async function getFromOpenGraphIo(url) {
    try {
        const proxyUrl = `https://opengraph.io/api/1.1/site/${encodeURIComponent(url)}?accept_lang=auto&use_proxy=true&app_id=${OPEN_GRAPH_API_KEY}`;
        const response = await fetch(proxyUrl);
        if (!response.ok) {
            throw new Error(`HTTP error! status: ${response.status}`);
        }
        const data = await response.json();
        console.log('Raw data from OpenGraph.io:', data); // Debugging step

        const ogProperties = {};

        if (data.hybridGraph) {
            ogProperties.image = data.hybridGraph.image;
            ogProperties.site_name = data.hybridGraph.site_name;
            ogProperties.title = data.hybridGraph.title;
            ogProperties.url = data.hybridGraph.url;
        }

        console.log('Fetched OpenGraph properties:', ogProperties);
        return ogProperties;
    } catch (error) {
        console.error('Error fetching OpenGraph properties:', error);
        return null;
    }
}

However, I cannot for the life of me call this object. What is the precise syntax? Am I importing it as a script into embedded HTML? (Doesn’t seem to work.)

Am I calling it inside a codeblock with the tag js-engine or @RunJS or whatever?

While I am at it, where can you save .env tokens like API keys? That is also not answered anywhere. In this function I am calling OpenGraph.io API, and I don’t want that key to be publicly accessible, yet I need to use a proxy api in order to get OpenGraph objects reliably.

I just want a very specific set of files as examples: a markdown file that calls a javascript function from a javascript file. Renders inside Edit and Read modes, and on publish.

Thanks for your time.

Given the context of my question here:

I want to know the directory structure, where to put what files, how to call them, so that they all work together.

Sorry, this is beyond my skillset.

Most of this is beyond the scope of the Custom CSS & Theme Design category, so I’ll close this topic. Let’s continue here for now: