Skip to content

Commit

Permalink
feat(vitepress): support inject global
Browse files Browse the repository at this point in the history
  • Loading branch information
pearmini committed Feb 29, 2024
1 parent b663aea commit bf1f0c5
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { Theme } from "../../../src";
import { createTheme } from "../../../src";

function display(fn) {
return fn();
}

const Theme = createTheme({
global: { display },
});

export default {
extends: Theme,
Expand Down
8 changes: 6 additions & 2 deletions packages/genji-theme-vitepress/__tests__/markdown-examples.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Genji Markdown Extension
# Genji Markdown Extensions

Render a red block.
Test Genji' built-in Markdown Extensions.

## Basic Block

It should render a red block.

```js
display(() => {
Expand Down
34 changes: 19 additions & 15 deletions packages/genji-theme-vitepress/src/Layout.vue
Original file line number Diff line number Diff line change
@@ -1,37 +1,41 @@
<script setup>
import { useRoute } from "vitepress";
import DefaultTheme from "vitepress/theme";
import { onMounted, watch } from "vue";
import * as stdlib from "./stdlib";
import { onMounted, watch, defineProps } from "vue";
const { Layout } = DefaultTheme;
const { global = {} } = defineProps(["global"]);
const route = useRoute();
watch(
() => route.path,
() => setTimeout(() => render())
);
onMounted(() => {
injectStdlib();
injectGlobal();
render();
});
function injectStdlib() {
Object.assign(window, stdlib);
function injectGlobal() {
Object.assign(window, global);
}
function render() {
const block = document.getElementsByClassName("language-js")[0];
if (!block) return;
const pre = document.getElementsByClassName("shiki")[0];
if (!pre) return;
const code = pre.textContent.replace(/\n/g, "");
const node = new Function(`return ${code}`)();
const cell = document.createElement("div");
cell.classList.add("genji-cell");
cell.appendChild(node);
block.parentNode.insertBefore(cell, block);
const blocks = document.getElementsByClassName("language-js");
if (!blocks.length) return;
for (const block of blocks) {
const pre = block.getElementsByClassName("shiki")[0];
if (!pre) return;
const code = pre.textContent.replace(/\n/g, "");
const node = new Function(`return ${code}`)();
const cell = document.createElement("div");
cell.classList.add("genji-cell");
cell.appendChild(node);
block.parentNode.insertBefore(cell, block);
}
}
</script>

Expand Down
2 changes: 1 addition & 1 deletion packages/genji-theme-vitepress/src/index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { default as Theme } from "./theme";
export { createTheme } from "./theme";
3 changes: 0 additions & 3 deletions packages/genji-theme-vitepress/src/stdlib/display.js

This file was deleted.

1 change: 0 additions & 1 deletion packages/genji-theme-vitepress/src/stdlib/index.js

This file was deleted.

13 changes: 9 additions & 4 deletions packages/genji-theme-vitepress/src/theme.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { h } from "vue";
import Layout from "./Layout.vue";
import DefaultTheme from "vitepress/theme";
import "./style.css";

export default {
extends: DefaultTheme,
Layout: Layout,
};
export function createTheme({ global } = {}) {
return {
extends: DefaultTheme,
Layout: () => {
return h(Layout, { global });
},
};
}

0 comments on commit bf1f0c5

Please sign in to comment.