From 59d9211c6584a9c4dd3ff8059708685f63c65c6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Ch=C3=A1varri?= Date: Tue, 12 Mar 2024 17:09:11 +0100 Subject: [PATCH] fix scroll to location hash (#170) --- docs/.vitepress/config.mts | 9 +++++++++ docs/.vitepress/toggleSyntax.js | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 docs/.vitepress/toggleSyntax.js diff --git a/docs/.vitepress/config.mts b/docs/.vitepress/config.mts index 74807ff7a..bd65dfafd 100644 --- a/docs/.vitepress/config.mts +++ b/docs/.vitepress/config.mts @@ -2,6 +2,8 @@ import { readFileSync } from "fs"; import { join } from "path"; import { defineConfig } from "vitepress"; +const toggleSyntaxScript = readFileSync(join(__dirname, './toggleSyntax.js'), 'utf8'); + // From https://github.com/ocamllabs/vscode-ocaml-platform/blob/master/syntaxes/reason.json const reasonGrammar = JSON.parse( readFileSync(join(__dirname, "./reasonml.tmLanguage.json"), "utf8") @@ -12,6 +14,13 @@ const base = process.env.BASE || "unstable"; // https://vitepress.dev/reference/site-config export default defineConfig({ title: "Melange Documentation Site", + head:[ + [ + 'script', + {}, + toggleSyntaxScript + ] + ], description: "The official documentation site for Melange, a compiler from OCaml to JavaScript. Explore the features and resources for functional programming with Melange, including the standard libraries APIs, the playground, and extensive documentation about bindings, build system, and the opam package manager.", base: `/${base}/`, diff --git a/docs/.vitepress/toggleSyntax.js b/docs/.vitepress/toggleSyntax.js new file mode 100644 index 000000000..2b0dbb460 --- /dev/null +++ b/docs/.vitepress/toggleSyntax.js @@ -0,0 +1,19 @@ +// Keep in sync with Switch.vue +// Just needed so that Vitepress finds the content already in the right +// position when it scrolls to location hash +document.addEventListener("DOMContentLoaded", () => { + const SYNTAXES = ["reasonml", "ocaml"]; + const CLASS_PREFIX = "syntax__"; + let currentSyntax; + let setCurrentSyntax = (syntax = SYNTAXES[0]) => { + document.body.classList.remove(`${CLASS_PREFIX}${currentSyntax}`); + document.body.classList.add(`${CLASS_PREFIX}${syntax}`); + + currentSyntax = syntax; + localStorage.setItem("syntax", currentSyntax); + }; + + setCurrentSyntax( + localStorage.getItem("syntax", currentSyntax) || SYNTAXES[0] + ); +});