diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..d1bcda8 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "docs/js/markbb"] + path = docs/js/markbb + url = https://github.com/OmniX-Space/markbb.git diff --git a/docs/js/index.js b/docs/js/index.js index e69de29..383da8e 100644 --- a/docs/js/index.js +++ b/docs/js/index.js @@ -0,0 +1,142 @@ +function route() { + const currentHash = window.location.hash; + const isDirectAccess = currentHash === "" || currentHash === "#" || currentHash === "#/"; + + if (isDirectAccess) { + const browserLang = getBrowserLanguage(); + setHtmlLangAttribute(browserLang); + window.location.href = "#/" + browserLang + "/v0.1.x/index.html"; + return; + } + + loadPageContent(currentHash.substring(1)); +} + +async function loadPageContent(path) { + try { + const mdPath = convertHtmlPathToMdPath(path); + + const markdownContent = await fetchMarkdownContent(mdPath); + + renderMarkdownContent(markdownContent); + + updatePageMetadata(path); + } catch (error) { + console.error("Failed to load page content: ", error); + renderErrorPage(error.message); + } +} + +function convertHtmlPathToMdPath(htmlPath) { + let normalizedPath = htmlPath.startsWith('/') ? htmlPath.substring(1) : htmlPath; + const mdFilename = normalizedPath.replace(/\.html$/, '.md'); + return `/markdown/${mdFilename}`; +} + +async function fetchMarkdownContent(mdPath) { + const response = await fetch(mdPath); + if (!response.ok) { + throw new Error(`Unable to load file: ${mdPath} with status code: ${response.status}`); + } + return await response.text(); +} + +function renderMarkdownContent(markdownContent) { + if (typeof marked === 'undefined') { + throw new Error('Markdown parsing library marked is not loaded, please introduce marked-js first.'); + } + + const htmlContent = marked.parse(markdownContent); + + const contentContainer = document.getElementById('content'); + if (!contentContainer) { + throw new Error('Content container with id "content" not found.'); + } + + contentContainer.innerHTML = htmlContent; + + handleInternalLinks(); +} + +function handleInternalLinks() { + const contentContainer = document.getElementById('content'); + const links = contentContainer.querySelectorAll('a'); + + links.forEach(link => { + const href = link.getAttribute('href'); + if (href && (href.endsWith('.md') || href.includes('/'))) { + link.addEventListener('click', (e) => { + e.preventDefault(); + const htmlPath = href.replace(/\.md$/, '.html'); + const normalizedPath = htmlPath.startsWith('/') ? htmlPath : '/' + htmlPath; + window.location.hash = normalizedPath; + route(); + }); + } + }); +} + +function updatePageMetadata(path) { + const pageTitle = path.split('/').pop().replace('.html', '').replace(/-/g, ' '); + document.title = `${capitalizeFirstLetter(pageTitle)}`; +} + +function capitalizeFirstLetter(string) { + return string.charAt(0).toUpperCase() + string.slice(1); +} + +function renderErrorPage(message) { + const contentContainer = document.getElementById('content'); + contentContainer.innerHTML = ` +
+

页面加载失败

+

${message}

+ +
+ `; +} + +// Load layout script +function loadLayoutScript() { + return new Promise((resolve, reject) => { + const script = document.createElement('script'); + script.src = "/js/layout.js"; + script.async = true; + + script.onload = () => { + resolve(); + script.remove(); + }; + + script.onerror = () => { + const errorMsg = `Failed to load script: ${scriptUrl}`; + console.error(`[ScriptLoader] ${errorMsg}`); + reject(new Error(errorMsg)); + }; + + document.body.appendChild(script); + }); +} + +// Initialize app +async function initializeApp() { + try { + await Promise.all([ + loadLayoutScript() + ]); + await Promise.all([ + loadScript("/js/i18n.js"), + loadCSSAsPromise("/css/layout.css"), + loadCSSAsPromise("/css/index.css"), + loadCSSAsPromise("/font-awesome/css/all.min.css") + ]) + createPage(); + route(); + } catch (error) { + console.error('App initialization error:', error); + } +} + +document.addEventListener('DOMContentLoaded', () => { + initializeApp(); +}); \ No newline at end of file diff --git a/docs/js/markbb b/docs/js/markbb new file mode 160000 index 0000000..173c175 --- /dev/null +++ b/docs/js/markbb @@ -0,0 +1 @@ +Subproject commit 173c175c0e10b589b016d933fa07787bbe26f90f