utils.js 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. export const inBrowser = typeof window !== 'undefined';
  2. /**
  3. * Converts a url path to the corresponding js chunk filename.
  4. */
  5. export function pathToFile(path) {
  6. let pagePath = path.replace(/\.html$/, '');
  7. if (pagePath.endsWith('/')) {
  8. pagePath += 'index';
  9. }
  10. if (import.meta.env.DEV) {
  11. // awlays force re-fetch content in dev
  12. pagePath += `.md?t=${Date.now()}`;
  13. }
  14. else {
  15. // in production, each .md file is built into a .md.js file following
  16. // the path conversion scheme.
  17. // /foo/bar.html -> ./foo_bar.md
  18. if (inBrowser) {
  19. const base = import.meta.env.BASE_URL;
  20. pagePath = pagePath.slice(base.length).replace(/\//g, '_') + '.md';
  21. // client production build needs to account for page hash, which is
  22. // injected directly in the page's html
  23. const pageHash = __VP_HASH_MAP__[pagePath];
  24. pagePath = `${base}_assets/${pagePath}.${pageHash}.js`;
  25. }
  26. else {
  27. // ssr build uses much simpler name mapping
  28. pagePath = `./${pagePath.slice(1).replace(/\//g, '_')}.md.js`;
  29. }
  30. }
  31. return pagePath;
  32. }