Skip to content

Commit 4ad1377

Browse files
committed
feat: change heading style, add heading component
1 parent 9eddcfe commit 4ad1377

File tree

3 files changed

+48
-45
lines changed

3 files changed

+48
-45
lines changed

src/assets/css/markdown.styl

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
dualr(--block-extend, -0.5rem, -1rem);
66

77
schemer(--hr-color, lighten(black, 82%), lighten(black, 50%));
8-
schemer(--heading-color, lighten($text-color, 10%), darken($text-color-d, 7%));
98
schemer(--bold-color, darken($theme-color, 20%), $theme-color);
109

1110
schemer(--checkbox-background, none, #cecece);
@@ -52,24 +51,7 @@ schemer(--quote-background-invert, 0, 1);
5251
.katex-display
5352
margin: 0.3em 2px;
5453

55-
/* 锚点 */
56-
.header-anchor
57-
opacity: 0;
58-
font-family: "Georgia", "Times New Roman", serif;
59-
color: lighten(black, 65%);
60-
transition: color 0.1s, opacity 0.15s;
61-
user-select: none;
62-
padding: 0 7px;
63-
display: inline-block;
64-
margin: 0;
65-
66-
&:hover
67-
color: darken($theme-color, 20%);
68-
69-
.heading:hover .header-anchor
70-
opacity: 1;
71-
72-
/* header 标签(h1 被 ban 掉了) */
54+
/* header 标签(h1被ban掉了) */
7355
h2
7456
font-size: 1.6rem;
7557
line-height: 2.6rem;
@@ -90,12 +72,6 @@ schemer(--quote-background-invert, 0, 1);
9072
font-size: 0.875rem;
9173
line-height: 1.25rem;
9274

93-
.heading
94-
position: relative;
95-
margin: 1.5rem 0;
96-
color: var(--heading-color);
97-
font-weight: 500;
98-
9975
/* hr 标签 */
10076
hr
10177
margin: 3.2rem auto;

src/components/Content.vue

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script setup lang="tsx">
22
/** @todo 图片缓存,不能每次都重新加载一遍 */
33
4-
import { computed, nextTick, onMounted, onUpdated } from "vue";
4+
import { computed, nextTick, onMounted } from "vue";
55
import { RouterView, useRoute } from "vue-router";
66
import { navigate } from "@/assets/ts/utils";
77
@@ -43,32 +43,13 @@ const show_comment: Ref<boolean> = computed(() => {
4343
return props.meta.attr.comment;
4444
});
4545
46-
/**
47-
* Iterate through headings and register anchor click event.
48-
*/
49-
const registerAnchor = () => {
50-
const headings = document.querySelectorAll(".heading");
51-
52-
headings.forEach((heading) => {
53-
const anchor = heading.querySelector(".header-anchor");
54-
if (anchor) {
55-
anchor.addEventListener("click", (e: Event) => {
56-
e.preventDefault();
57-
navigate(heading.id);
58-
});
59-
}
60-
});
61-
};
62-
6346
const scrollToAnchor = async () => {
6447
await nextTick();
6548
const hash = route.hash;
6649
navigate(hash.slice(1), false);
6750
};
6851
6952
onMounted(scrollToAnchor);
70-
onMounted(registerAnchor);
71-
onUpdated(registerAnchor);
7253
</script>
7354

7455
<template>

src/components/md/Heading.vue

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<script setup lang="ts">
2+
import { navigate } from "@/assets/ts/utils";
3+
4+
const props = defineProps<{ level: number; id: number }>();
5+
6+
const anchorClick = (e: Event) => {
7+
e.preventDefault();
8+
navigate(props.id.toString());
9+
};
10+
</script>
11+
12+
<template>
13+
<component :is="`h${level}`" :id="id" :tabindex="-1" class="heading">
14+
<a :href="`#${id}`" class="anchor" @click="anchorClick">#</a>
15+
<slot />
16+
</component>
17+
</template>
18+
19+
<style scoped lang="stylus">
20+
@import "../../assets/css/global.styl";
21+
22+
$anchor-width = 1.65rem;
23+
24+
.heading
25+
scheme(--heading-color, lighten($text-color, 10%), darken($text-color-d, 7%));
26+
dual(--heading-margin-left, (- $anchor-width), 0);
27+
28+
position: relative;
29+
margin: 1.5rem 0;
30+
color: var(--heading-color);
31+
font-weight: 500;
32+
33+
> .anchor
34+
font-family: $monospace;
35+
color: lighten(black, 65%);
36+
transition: color 0.1s;
37+
user-select: none;
38+
display: inline-block;
39+
margin: 0;
40+
width: $anchor-width;
41+
margin-left: var(--heading-margin-left);
42+
font-size: 1.05em;
43+
44+
&:hover
45+
color: darken($theme-color, 20%);
46+
</style>

0 commit comments

Comments
 (0)