Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: add heading tag anchors #12

Open
ninest opened this issue May 24, 2020 · 0 comments
Open

Suggestion: add heading tag anchors #12

ninest opened this issue May 24, 2020 · 0 comments

Comments

@ninest
Copy link

ninest commented May 24, 2020

I realized that the blog didn't have heading anchors, so I thought it'll be a good idea to add to the example.

The markdown-it-anchor plugin can be used.

In nuxt.config.js, add:

import mia from 'markdown-it-anchor';

...

md.use(mia, {
  permalink: true,
});

...

export default {
  ...
  router: {
    scrollBehavior: async (to, from, savedPosition) => {
      if (savedPosition) {
        return savedPosition;
      }

      const findEl = (hash, x = 0) => {
        return (
          document.querySelector(hash) ||
          new Promise((resolve) => {
            if (x > 50) {
              return resolve(document.querySelector('#app'));
            }
            setTimeout(() => {
              resolve(findEl(hash, ++x || 1));
            }, 100);
          })
        );
      };

      if (to.hash) {
        const el = await findEl(to.hash);
        if ('scrollBehavior' in document.documentElement.style) {
          return window.scrollTo({ top: el.offsetTop, behavior: 'smooth' });
        } else {
          return window.scrollTo(0, el.offsetTop);
        }
      }

      return { x: 0, y: 0 };
    }
  }
};

Anchors seem to be broken in Nuxt, so you need to add the scrollBehavior function. Check this issue: #5359

Another reason why this might be a good idea to add is because anchors don't work in Nuxt. This may help beginners.

Apart from this, I'd also like to thank you for creating this example! It's perfect!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant