allow routing

This commit is contained in:
koehr 2023-12-06 22:14:35 +01:00 committed by Norman Köhring
parent 937b479f0d
commit aef7cd3e05
6 changed files with 68 additions and 7 deletions

View file

@ -1,12 +1,12 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, computed, onMounted, watch } from 'vue' import { ref, computed, onMounted, watch } from 'vue'
import { useData } from 'vitepress' import { useData, useRouter } from 'vitepress'
import useTerminal from './useTerminal' import useTerminal from './useTerminal'
import titleArt from './titles' import titleArt from './titles'
// https://vitepress.dev/reference/runtime-api#usedata const { site, page, frontmatter } = useData()
const { site, frontmatter } = useData()
const enhancedReadability = ref(false) const enhancedReadability = ref(false)
const router = useRouter()
const title = computed(() => { const title = computed(() => {
const titleKey = frontmatter.value.title const titleKey = frontmatter.value.title
@ -31,7 +31,15 @@ onMounted(() => {
const { addText, addLine, clear, footerLinks } = useTerminal(textArea.value, commands.value) const { addText, addLine, clear, footerLinks } = useTerminal(textArea.value, commands.value)
watch(router.route, async () => {
if (page.value.isNotFound) {
addText('\n', false)
await router.go('404')
}
}, { immediate: true })
watch(frontmatter, () => { watch(frontmatter, () => {
if (page.value.isNotFound) return
addText(title.value + '\n', false) addText(title.value + '\n', false)
addLine(content.value.join('\n')) addLine(content.value.join('\n'))
}, { immediate: true }) }, { immediate: true })

View file

@ -19,4 +19,11 @@ export default {
"| <| -__|__ --| | | | -__|", "| <| -__|__ --| | | | -__|",
"|___|__||_____|_____|_____|__|__|__|_____|", "|___|__||_____|_____|_____|__|__|__|_____|",
], ],
not_found: [
" _____ ______ _____ ",
"| | || | | | ",
"|__ | -- |__ |",
" |__||______| |__| ",
]
} }

View file

@ -1,10 +1,14 @@
import { ref } from 'vue' import { ref } from 'vue'
import type { SimpleCommand, Uri } from './Config' import type { SimpleCommand, Uri } from './Config'
import { useRouter } from 'vitepress'
export default function useTerminal(inputEl: HTMLTextAreaElement, commands: SimpleCommand[]) { export default function useTerminal(inputEl: HTMLTextAreaElement, commands: SimpleCommand[]) {
const prompt = '\n$> ' const prompt = '\n$> '
const footerLinks = ref([]) const footerLinks = ref([])
const router = useRouter()
console.log('router', router)
function moveCursorToEnd() { function moveCursorToEnd() {
const pos = inputEl.value.length const pos = inputEl.value.length
@ -36,7 +40,7 @@ export default function useTerminal(inputEl: HTMLTextAreaElement, commands: Simp
addText('') addText('')
} }
type SYS_OUT = 'NOT_FOUND' | 'USAGE' | 'INFO' type SYS_OUT = 'NOT_FOUND' | 'USAGE' | 'INFO' | '404'
const SHELL = 'k0rSH' const SHELL = 'k0rSH'
const INFO = 'k0rSH v0.1: the k0r SHell, fiddled together by k0r -- https://k0r.in' const INFO = 'k0rSH v0.1: the k0r SHell, fiddled together by k0r -- https://k0r.in'
@ -67,6 +71,10 @@ export default function useTerminal(inputEl: HTMLTextAreaElement, commands: Simp
console.log('explaining myself') console.log('explaining myself')
addLine(`${SHELL}: ${INFO}`) addLine(`${SHELL}: ${INFO}`)
break break
case '404':
console.log('page not found', arg)
addLine(`${SHELL}: ${arg}: this page does not exist`)
break
} }
} }
@ -78,12 +86,14 @@ export default function useTerminal(inputEl: HTMLTextAreaElement, commands: Simp
footerLinks.value = uris footerLinks.value = uris
} }
/// returns current command written in the command line
/// in the format: [command, arg1, arg2, ..., argN]
function getCurrentCommand() { function getCurrentCommand() {
const value = inputEl.value const value = inputEl.value
const start = value.lastIndexOf(prompt) + prompt.length const start = value.lastIndexOf(prompt) + prompt.length
const end = value.length const end = value.length
return value.slice(start, end).trim() return value.slice(start, end).trim().split(' ')
} }
function execUserCommand(cmd: string) { function execUserCommand(cmd: string) {
@ -98,8 +108,16 @@ export default function useTerminal(inputEl: HTMLTextAreaElement, commands: Simp
setFooter(userCommand.uris) setFooter(userCommand.uris)
} }
function listPages() {
addLine('TODO: list pages')
}
async function openPage(page: string) {
await router.go(page)
}
function handleCurrentCommand() { function handleCurrentCommand() {
const cmd = getCurrentCommand() const [cmd, ...args] = getCurrentCommand()
if (!cmd) { if (!cmd) {
addText('') addText('')
@ -117,6 +135,16 @@ export default function useTerminal(inputEl: HTMLTextAreaElement, commands: Simp
case 'clear': case 'clear':
clear() clear()
break break
case 'ls':
case 'list':
listPages()
break
case 'go':
case 'open':
addText('\n', false)
if (!args.length) addText('USAGE: go page_name')
else router.go(args[0])
break
default: default:
execUserCommand(cmd) execUserCommand(cmd)
} }

6
404.md Normal file
View file

@ -0,0 +1,6 @@
---
title: 'not_found'
content: [
'This page does not exist.',
]
---

9
cv.md Normal file
View file

@ -0,0 +1,9 @@
---
title: 'resume'
content: [
'This is my CV',
'stuff',
'foo',
'bar',
]
---

View file

@ -1,5 +1,4 @@
--- ---
home: true
title: 'welcome' title: 'welcome'
content: [ content: [
'This is the homepage of Norman Köhring,', 'This is the homepage of Norman Köhring,',
@ -11,3 +10,7 @@ content: [
'Some commands might update the footer with useful links.' 'Some commands might update the footer with useful links.'
] ]
--- ---
Some testcontent for testing.
It is a *paragraph*!