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

Adding custom styles #292

Closed
leem32 opened this issue Mar 21, 2019 · 4 comments
Closed

Adding custom styles #292

leem32 opened this issue Mar 21, 2019 · 4 comments

Comments

@leem32
Copy link

leem32 commented Mar 21, 2019

I'd like to add some custom styles to make remark fit better with my page.
According to this remark issue, it's possible to add a custom stylesheet like this:

  var remark_config = {
    site_id: 'YOUR_SITE_ID',
    ...
    style: "https://somehost.com/remark.css"
  };

But it doesn't seem to be working for me. Ive tried both relative and absolute URL's e.g https://somehost.com/css/remark.css and /css/remark.cs, ./css/remark.css.

Am I doing it wrong?

@umputun
Copy link
Owner

umputun commented Mar 21, 2019

#267 you mentioned is work in progress. I'm not sure if passing style supported in the current version. @Reeywhaar - am I right, or right ;)

@Reeywhaar
Copy link
Collaborator

Reeywhaar commented Mar 21, 2019

@umputun no, you're right! I made bare minimum in refactoring, to avoid long review (still there is), I am planning to make other features after refactoring will be accepted (#279, external styles, #285, etc...)

@leem32 it's not possible at this moment, unfortunately

@umputun
Copy link
Owner

umputun commented Mar 21, 2019

closed as dup #5

@umputun umputun closed this as completed Mar 21, 2019
@leem32
Copy link
Author

leem32 commented Mar 22, 2019

I thought that might be the case.

In the meantime, I'll just add the styles using pure JS instead.

Here's a couple of examples on how to do that for others who want to add custom styles. They work by first detecting when the iframe is loaded in the dom and then uses an eventListener to detect when the iframe content has loaded and injects the stylesheet into the head of the iframe html..

From my tests both seem to work equally well so just choose the one you prefer.

Example 1

var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
var observer = new MutationObserver(function(mutations) {

    mutations.forEach(function (mutation) {

        [].filter.call(mutation.addedNodes, function (node) {

            return node.nodeName == 'IFRAME';

        }).forEach(function (node) {

            node.addEventListener('load', function (e) {

                console.log("iframe content loaded");

                var iframe = document.querySelector('#remark42 iframe');

                // style iframe element
                // iframe.style.border = "5px solid aqua";

                var style = document.createElement('style');
                style.id = "custom-remark-styles";

                // style.textContent =
                //   'body {' +
                //   '  background-color: green;' +
                //   '  font-weight: italic;' +
                //   '}'
                // ;

                style.textContent = "body { background-color: green; font-weight: italic; }\n" +
                                    "p { padding-bottom: 0; padding-top: 0; }\n" +
                                    "h1 { margin-top: 0; margin-bottom: 0; }\n" +
                                    "div { padding-bottom: 0; padding-top: 0; }";

                iframe.contentDocument.head.appendChild(style);
                console.log(iframe.contentDocument.head);

                // and disconnect
                observer.disconnect();

            });
        });
    });
});

var targetNode = document.getElementById("remark42");
observer.observe(targetNode, { childList: true, subtree: true });

// also works
// observer.observe(document.body, { childList: true, subtree: true });

Example 2
This example also uses the MutationObserver constructor but is more readable.

var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
var observer = new MutationObserver(function(mutations) {

    if (mutations[0].type === "childList") { // optional

        if (document.querySelector("#remark42 iframe")) {
            
            console.log("iframe added to dom");

            var iframe = document.querySelector('#remark42 iframe');

            iframe.addEventListener('load', function (e) {

                console.log("iframe content loaded");

                // style iframe element
                // iframe.style.border = "5px solid aqua";

                var style = document.createElement('style');
                style.id = "custom-remark-styles";

                // style.textContent =
                //   'body {' +
                //   '  background-color: green;' +
                //   '  font-weight: italic;' +
                //   '}'
                // ;

                style.textContent = "body { background-color: green; font-weight: italic; }\n" +
                                    "p { padding-bottom: 0; padding-top: 0; }\n" +
                                    "h1 { margin-top: 0; margin-bottom: 0; }\n" +
                                    "div { padding-bottom: 0; padding-top: 0; }";

                iframe.contentDocument.head.appendChild(style);
                // console.log(iframe.contentDocument.head);

                // and disconnect
                observer.disconnect();

            });
        }
      }
});

var observerConfig = {
    childList: true,
};

var targetNode = document.getElementById("remark42");
observer.observe(targetNode, observerConfig);

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

3 participants