如何在URL中添加参数以在docsify中切换暗/亮模式

huangapple go评论80阅读模式
英文:

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

问题

我有一个使用docsify制作文档的网站。现在我想根据我的网站状态在暗/亮模式之间切换文档。我在文档中没有找到任何相关内容。

是否可以向URL添加参数以启动docsify站点的暗模式?

我使用以下脚本来实现暗模式:

<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"
/>
英文:

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:

&lt;script
    src=&quot;//cdn.jsdelivr.net/npm/docsify-darklight-theme@latest/dist/index.min.js&quot;
    type=&quot;text/javascript&quot;&gt;
&lt;/script&gt;

&

&lt;link
    rel=&quot;stylesheet&quot;
    href=&quot;//cdn.jsdelivr.net/npm/docsify-darklight-theme@latest/dist/style.min.css&quot;
    title=&quot;docsify-darklight-theme&quot;
    type=&quot;text/css&quot;
/&gt;

答案1

得分: 0

我添加了这段代码到加载主题脚本之前的脚本部分:

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

现在我可以在我的URL中添加一个主题参数来改变文档的主题:
http://DOMAIN/docsify/#/?&theme=dark

英文:

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

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

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

huangapple
  • 本文由 发表于 2023年6月22日 14:50:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76529246.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定