@@ -16,7 +16,7 @@ export interface Doc {
1616 }
1717}
1818
19- const marked = new Marked (
19+ export const marked = new Marked (
2020 markedHighlight ( {
2121 emptyLangClass : "hljs" ,
2222 langPrefix : "hljs language-" ,
@@ -27,7 +27,7 @@ const marked = new Marked(
2727 } ) ,
2828)
2929
30- const renderer = new marked . Renderer ( )
30+ export const renderer = new marked . Renderer ( )
3131
3232renderer . code = function ( { text } ) {
3333 const parts = text . split ( "\n" )
@@ -51,62 +51,64 @@ renderer.blockquote = function ({ tokens }) {
5151 return `<blockquote class="my-6 px-6 pa-4 border-s-lg border-primary rounded" style="--v-border-opacity: 1; background: rgba(var(--v-theme-primary), 0.12);">${ content } </blockquote>`
5252}
5353
54- export const useDocs = createGlobalState ( ( ) => {
55- // const router = useRouter()
54+ function getIdFromTitle ( title : string ) {
55+ let id = ""
56+ for ( const char of title . toLowerCase ( ) ) {
57+ const code = char . charCodeAt ( 0 )
58+ const isAlpha = code >= 97 && code <= 122
59+ const isAllowedSpace = code === 32 && id . length > 0 && id [ id . length - 1 ] !== " "
60+ if ( isAlpha || isAllowedSpace ) {
61+ id += char
62+ }
63+ }
5664
57- return useAsyncState ( async ( ) => {
58- const res = await fetch ( "/docs/docs.yaml" )
59- const data = await res . text ( )
60- const docs = yaml . load ( data ) as Doc [ ]
61- const contents = await Promise . all (
62- docs
63- . map ( doc => fetch ( `/docs/${ doc . file } ` )
64- . then ( res => res . text ( ) ) ) ,
65- )
65+ return id . replaceAll ( " " , "-" )
66+ }
6667
67- let tableOfContent : Doc [ "md" ] [ "tableOfContent" ] = [ ]
68-
69- const linkIcon = `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path fill="currentColor" d="M10.59 13.41c.41.39.41 1.03 0 1.42c-.39.39-1.03.39-1.42 0a5.003 5.003 0 0 1 0-7.07l3.54-3.54a5.003 5.003 0 0 1 7.07 0a5.003 5.003 0 0 1 0 7.07l-1.49 1.49c.01-.82-.12-1.64-.4-2.42l.47-.48a2.98 2.98 0 0 0 0-4.24a2.98 2.98 0 0 0-4.24 0l-3.53 3.53a2.98 2.98 0 0 0 0 4.24m2.82-4.24c.39-.39 1.03-.39 1.42 0a5.003 5.003 0 0 1 0 7.07l-3.54 3.54a5.003 5.003 0 0 1-7.07 0a5.003 5.003 0 0 1 0-7.07l1.49-1.49c-.01.82.12 1.64.4 2.43l-.47.47a2.98 2.98 0 0 0 0 4.24a2.98 2.98 0 0 0 4.24 0l3.53-3.53a2.98 2.98 0 0 0 0-4.24a.973.973 0 0 1 0-1.42"/></svg>`
70- renderer . heading = function ( { depth, tokens } ) {
71- const content = this . parser . parseInline ( tokens )
72- let id = ""
73- let a = ""
74- if ( depth === 2 ) {
75- id = content . toLowerCase ( ) . replaceAll ( ":" , "" ) . replaceAll ( " " , "-" )
76- a = `<a href="#${ id } " class="d-inline-block mr-2 text-primary opacity-50">${ linkIcon } </a>`
77- tableOfContent . push ( { id, content } )
78- }
68+ let tableOfContent : Doc [ "md" ] [ "tableOfContent" ] = [ ]
69+
70+ const linkIcon = `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path fill="currentColor" d="M10.59 13.41c.41.39.41 1.03 0 1.42c-.39.39-1.03.39-1.42 0a5.003 5.003 0 0 1 0-7.07l3.54-3.54a5.003 5.003 0 0 1 7.07 0a5.003 5.003 0 0 1 0 7.07l-1.49 1.49c.01-.82-.12-1.64-.4-2.42l.47-.48a2.98 2.98 0 0 0 0-4.24a2.98 2.98 0 0 0-4.24 0l-3.53 3.53a2.98 2.98 0 0 0 0 4.24m2.82-4.24c.39-.39 1.03-.39 1.42 0a5.003 5.003 0 0 1 0 7.07l-3.54 3.54a5.003 5.003 0 0 1-7.07 0a5.003 5.003 0 0 1 0-7.07l1.49-1.49c-.01.82.12 1.64.4 2.43l-.47.47a2.98 2.98 0 0 0 0 4.24a2.98 2.98 0 0 0 4.24 0l3.53-3.53a2.98 2.98 0 0 0 0-4.24a.973.973 0 0 1 0-1.42"/></svg>`
71+ renderer . heading = function ( { depth, tokens } ) {
72+ const content = this . parser . parseInline ( tokens )
73+ let id = ""
74+ let a = ""
75+ if ( depth === 2 ) {
76+ id = getIdFromTitle ( content )
77+ // .toLowerCase().replaceAll(":", "").replaceAll(" ", "-")
78+ a = `<a href="#${ id } " class="d-inline-block mr-2 text-primary opacity-50">${ linkIcon } </a>`
79+ tableOfContent . push ( { id, content } )
80+ }
7981
80- const lv = Math . min ( depth + 3 , 6 )
81- return `
82- <h${ depth } id="${ id } " class="text-h${ lv } mb-4">
82+ const lv = Math . min ( depth + 3 , 6 )
83+ return `
84+ <h${ depth } id="${ id } " class="text-white text- h${ lv } mb-4">
8385 ${ a } ${ content }
8486 </h${ depth } >
8587 `
86- }
88+ }
8789
88- renderer . paragraph = function ( { tokens } ) {
89- const content = this . parser . parseInline ( tokens )
90- return `<p class="text-body-1 text-accent mt-3 mb-6">${ content } </p>`
91- }
90+ renderer . paragraph = function ( { tokens } ) {
91+ const content = this . parser . parseInline ( tokens )
92+ return `<p class="text-body-1 text-accent mt-3 mb-6">${ content } </p>`
93+ }
9294
93- renderer . listitem = function ( x ) {
94- const content = this . parser . parse ( x . tokens )
95- return `
96- <li class="mb-2">
95+ renderer . listitem = function ( x ) {
96+ const content = this . parser . parse ( x . tokens )
97+ return `
98+ <li class="mb-2 text-white ">
9799 <span class="text-body-1 text-accent">${ content } </span>
98100 </li>
99101 `
100- }
102+ }
101103
102- renderer . list = function ( { ordered, items } ) {
103- const tag = ordered ? "ol" : "ul"
104- const body = items . map ( item => this . listitem ( item ) ) . join ( "" )
105- return `<${ tag } class="mt-4 mb-6 pl-4" style="list-style-type: square">${ body } </${ tag } >`
106- }
104+ renderer . list = function ( { ordered, items } ) {
105+ const tag = ordered ? "ol" : "ul"
106+ const body = items . map ( item => this . listitem ( item ) ) . join ( "" )
107+ return `<${ tag } class="mt-4 mb-6 pl-4" style="list-style-type: square">${ body } </${ tag } >`
108+ }
107109
108- renderer . link = function ( { href, tokens } ) {
109- const content = this . parser . parseInline ( tokens )
110+ renderer . link = function ( { href, tokens } ) {
111+ const content = this . parser . parseInline ( tokens )
110112
111113 // if (href.startsWith("/")) {
112114 // const a = document.createElement("a")
@@ -120,24 +122,39 @@ export const useDocs = createGlobalState(() => {
120122 // }
121123
122124 ; ( window as any ) . xonClick = function ( e : Event , _ : HTMLAnchorElement ) {
123- e . preventDefault ( )
124- // console.log({ event, target })
125- }
125+ e . preventDefault ( )
126+ // console.log({ event, target })
127+ }
126128
127- return `<a class="text-link" href="${ href } " onclick="xonClick(event, this);">${ content } </a>`
128- }
129+ return `<a class="text-link" href="${ href } " onclick="xonClick(event, this);">${ content } </a>`
130+ }
129131
130- renderer . strong = function ( { tokens } ) {
131- const content = this . parser . parseInline ( tokens )
132- return `<strong class="text-white text-body-1 font-weight-bold">${ content } </strong>`
133- }
132+ renderer . strong = function ( { tokens } ) {
133+ const content = this . parser . parseInline ( tokens )
134+ return `<strong class="text-white text-body-1 font-weight-bold">${ content } </strong>`
135+ }
134136
135- renderer . codespan = function ( { text } ) {
136- return `<code
137+ renderer . codespan = function ( { text } ) {
138+ return `<code
137139 class="text-primary text-body-1 border border-primary py-1 px-2 rounded"
138140 style="--v-border-opacity: 0.12; background-color: rgba(var(--v-theme-primary), var(--v-border-opacity))"
139141 >${ text } </code>`
140- }
142+ }
143+
144+ export const useDocs = createGlobalState ( ( ) => {
145+ // const router = useRouter()
146+
147+ return useAsyncState ( async ( ) => {
148+ const res = await fetch ( "/docs/docs.yaml" )
149+ const data = await res . text ( )
150+ const docs = yaml . load ( data ) as Doc [ ]
151+ const contents = await Promise . all (
152+ docs
153+ . map ( doc => fetch ( `/docs/${ doc . file } ` )
154+ . then ( res => res . text ( ) ) ) ,
155+ )
156+
157+ // let tableOfContent: Doc["md"]["tableOfContent"] = []
141158
142159 return docs . map ( ( doc , index ) => {
143160 tableOfContent = [ ]
0 commit comments