@@ -2,6 +2,8 @@ import { EditPage } from "./components/Editor.js"
22
33const $content = document . getElementById ( "content" )
44
5+ let BASE_PATH = ""
6+
57// 라우팅 처리 함수
68function matchRoute ( path ) {
79 if ( path === "/" ) return routes [ "/" ]
@@ -25,7 +27,7 @@ const routes = {
2527 $content . innerHTML = "<h1>문서 목록</h1>"
2628 // 예시: 목록을 클릭하면 특정 id 문서로 이동
2729 const link = document . createElement ( "a" )
28- link . href = " /documents/123"
30+ link . href = ` ${ BASE_PATH } /documents/123`
2931 link . textContent = "문서 123 보기"
3032 link . addEventListener ( "click" , ( e ) => {
3133 e . preventDefault ( )
@@ -44,7 +46,7 @@ const routes = {
4446}
4547
4648export function navigateTo ( path ) {
47- history . pushState ( { path } , "" , path )
49+ history . pushState ( { path } , "" , ` ${ BASE_PATH } ${ path } ` )
4850 loadContent ( path )
4951}
5052
@@ -61,5 +63,27 @@ window.addEventListener("popstate", (e) => {
6163
6264// 첫 로딩
6365window . addEventListener ( "DOMContentLoaded" , ( ) => {
64- loadContent ( location . pathname )
66+ const pathSegments = location . pathname . split ( "/" ) . filter ( Boolean )
67+ if ( location . hostname . endsWith ( "github.io" ) && pathSegments . length > 0 ) {
68+ BASE_PATH = `/${ pathSegments [ 0 ] } `
69+ } else {
70+ BASE_PATH = ""
71+ }
72+
73+ let initialBrowserPath = location . pathname
74+ let pathForRouter = "/"
75+
76+ if ( initialBrowserPath . startsWith ( BASE_PATH ) ) {
77+ pathForRouter = initialBrowserPath . substring ( BASE_PATH . length )
78+ if ( pathForRouter === "" ) {
79+ pathForRouter = "/"
80+ }
81+ }
82+ const restoredPath = history . state ?. path
83+ const finalPathToLoad =
84+ restoredPath && restoredPath . startsWith ( BASE_PATH )
85+ ? restoredPath . substring ( BASE_PATH . length ) || "/"
86+ : pathForRouter
87+
88+ loadContent ( finalPathToLoad )
6589} )
0 commit comments