docsify

How can I add in the URL to open docsify in dark/lightmode


I have a website that uses docsify for documentation. Now I want the documentation to open in dark/light mode depending on what state my site is in. I have not found anything in the documentation.

Is it possible to add a parameter to the URL to start the docsify site in darkmode?

I use this script for darkmode:

<script
    src="//cdn.jsdelivr.net/npm/docsify-darklight-theme@latest/dist/index.min.js"
    type="text/javascript">
</script>

&

<link
    rel="stylesheet"
    href="//cdn.jsdelivr.net/npm/docsify-darklight-theme@latest/dist/style.min.css"
    title="docsify-darklight-theme"
    type="text/css"
/>

Solution

  • I added this to my script section before loading the theme script:

    const theme = new URLSearchParams(window.location.href).get('theme');
    if(theme){
        const setTheme = theme == "dark" ? "dark" : "light";
        localStorage.setItem('DARK_LIGHT_THEME', setTheme);
    }
    

    Now I can add a theme parameter to my URL that changes the theme of the documentation: http://DOMAIN/docsify/#/?&theme=dark