question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ID generation with umlauts lead to bad links in table of contents

See original GitHub issue

Version

@nuxt/content: v1.11.1 nuxt: v2.14.12

Reproduction Link

https://codesandbox.io/s/jovial-cray-sphhr?file=/content/index.md:55-119

Steps to reproduce

On the index page, you can not follow the h2 anchor with the umlauts “Ü”.

What is Expected?

The anchor should be reachable.

What is actually happening?

The TOC-example snippet is correct, it encodes the anchor link to https://sphhr.sse.codesandbox.io/#über-uns---does-not-work

The output produced by <nuxt-content /> creates an ID to follow that is not escaped: id="über-uns---does-not-work".

There are two approaches:

a) Avoid URL escaping in the TOC snippet with <a :href="#${link.id}">{{ link.text }}</a> instead.

b) Strip all non-standard characters from TOC and ID generation.

Sadly even Github has its problem with the same topic: https://github.com/adrianrudnik/md-umlauts-test

Clicking the anchor icon in front produces #über-uns---does-not-work, right clicking the anchor icon > copy link will produce #%C3%BCber-uns---does-not-work, so no guidance there.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:4
  • Comments:6

github_iconTop GitHub Comments

3reactions
wirkcommented, Feb 11, 2021

I just ran into this issue as well and created a small hacky fix to throw into your _slug.vue till this gets properly fixed.

import {slugify} from 'transliteration'

function fixIds(elements = []) {
  const slugifyOptions = {replace: {ü: 'ue', ä: 'ae', ö: 'oe', ß: 'ss'}}
  elements.forEach((el) => {
    if (el.props && el.props.id) {
      el.props.id = slugify(el.props.id, slugifyOptions)
    }
    if (el.id) {
      el.id = slugify(el.id, slugifyOptions)
    }
    if (el.children) {
      fixIds(el.children)
    }
  })
}

export default {
  async asyncData({$content, params}) {
    const article = await $content('articles', params.slug).fetch()
    fixIds(article.body.children)
    fixIds(article.toc)

    return {article}
  },
...
1reaction
derawcommented, Dec 6, 2021

Hi,

I also ran into this issue and I completed @wirk’s solution to also slugify anchors:

import slugify from 'slugify';

interface Element {
	id?: string;
	props?: {
		[key: string]: string;
	};
	children?: Element[];
}

/** @see https://github.com/nuxt/content/issues/702 */
export function slugifyAnchors(elements: Element[] = []): void {
	elements.forEach((el) => {
		if (el.props?.id) {
			el.props.id = slugify(el.props.id);
		}

		if (el.props?.href) {
			const URI = decodeURI(el.props.href);

			const httpScheme = URI.startsWith('http://');
			const httpsScheme = URI.startsWith('https://');
			const isExternalURI = httpScheme || httpsScheme;

			const href = isExternalURI ? URI : '#' + slugify(URI);

			el.props.href = href;
		}

		if (el.id) {
			el.id = slugify(el.id);
		}

		if (el.children) {
			slugifyAnchors(el.children);
		}
	});
}

(Updated to allow external links)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Links containing umlaut or cyrillic characters become invalid
When you insert a link that contains umlaut or cyrillic characters via Insert more content > Link > Web link or the shortcuts...
Read more >
cassandra - Inserting data in table with umlaut is not possible
I get back the message from cqlsh: "Bad Request: Input length = 1" ... CREATE TABLE testdata ( id varchar, text varchar, PRIMARY...
Read more >
Working with Bibliographic Records
Overview and instructions for bibliographic record creation and management in the MD Editor.
Read more >
UTR #36: Unicode Security Considerations
Contents. 1 Introduction. 1.1 Structure. 2 Visual Security Issues. 2.1 Internationalized Domain Names. Table 1. Safe Domain Names. 2.2 Mixed-Script Spoofing.
Read more >
Links - Usability & Web Accessibility - Yale University
Table of Contents. How People with Disabilities Use Links; Link Text. Fallbacks. Image Links; Adjacent Links; Designing Links.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found