Ghost: Cant generate ID if heading is other language in markdown

Created on 7 Feb 2020  路  9Comments  路  Source: TryGhost/Ghost

Issue Summary

Cant generate ID if heading is other language in markdown

To Reproduce

  1. new post, add markdown;
  2. add heading with other language, e.g. Chinese;
  3. save post, heading has no ID.

Technical details:

  • Ghost Version: 3.5.0
  • Node Version: docker, ghost:3-alpine
  • Browser/OS: ubuntu 16.04
  • Database: mysql 3.7
editor help wanted server / core stale

All 9 comments

Yes, hope to solve this problem !

image

Agree,met with the same problem!

In Russian, a post with the name "袧邪蟹胁邪薪懈械" will be assigned the "/nazvaniie/" address (which is fine). It would be nice if the IDs were generated in the similar way as the post URL.

At the moment, IDs are empty or contain only English words, if they are present in the title.

A temporary way is to generate heading ids when opening the post with javascript, for example:

var contentSel = '.post-content-main' // depends on ghost theme

if(headings.length){
  // todo: to avoid ghost bug: can't generate heading id if heading is other than English
  for (var idx = 0 ; idx< headings.length; idx++){
    var newId = headings[idx].innerHTML + '-' + Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5)
    headings[idx].setAttribute('id', newId)
  }
}

https://github.com/TryGhost/Ghost/blob/c0512e30bb6e5e3f2d3d86a3ca04b0273d405447/core/server/lib/mobiledoc/converters/markdown-converter.js#L13

Changing this line to var slug = inputString.toLowerCase(); or something will fix this but it will break links which uses characters outside \w.

The slug generation when not using markdown cards differs from it.
These are neither non-latin-characters-friendly. (For example 袧邪蟹胁邪薪懈械 becomes -)

https://github.com/TryGhost/Ghost/blob/c0512e30bb6e5e3f2d3d86a3ca04b0273d405447/core/server/lib/mobiledoc/converters/mobiledoc-converter.js#L52-L58

In my opinion, the slug generation in this vscode's PR is good.

Markdown cards are the primary backwards compatibility mechanism for content from Ghost 0.x, we need to ensure the existing header id generation doesn't change so we don't break anchor links in legacy content. Similarly for the rich-text editor, changing the header id generation should not cause any changes for existing content.

What we can probably do is adapt the current id generation routines so that once an id is generated using the existing algorithm, if it's empty or only contains dashes then we can trigger a more non-latin-char friendly generator. Use of the @tryghost/string's slugify function may be useful there but I'd be interested to know of any approaches that perform better for non-latin languages.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

+1

Any updates on this?

Was this page helpful?
0 / 5 - 0 ratings