remarkjs / remark-slug Goto Github PK
View Code? Open in Web Editor NEWLegacy plugin to add `id`s to headings — please use `rehype-slug`
Home Page: https://remark.js.org
License: MIT License
Legacy plugin to add `id`s to headings — please use `rehype-slug`
Home Page: https://remark.js.org
License: MIT License
Not sure whether this belongs here or on remark-validate-links
, but given the following:
### :triangular_ruler: Example
GitHub generates #triangular_ruler-example
. However, remark-validate-links
expects #triangularruler-example
, with no underscore, using the following .remarkrc
:
{
"plugins": {
"toc": {
"tight": true
},
"slug": {
"library": "github"
},
"lint": {
"no-html": false,
"list-item-spacing": false,
"list-item-indent": "mixed",
"code-block-style": "fenced",
"maximum-heading-length": 80,
"no-heading-punctuation": false,
"no-missing-blank-lines": false
},
"validate-links": true
},
"settings": {
"listItemIndent": "1",
"bullet": "*"
}
}
Hi!
I am trying to switch from remark-html to remark-react with this:
// remark-plugins.js
import remark from 'remark';
import slug from "remark-slug"
import reactRenderer from 'remark-react';
export default (body: string) => remark()
.use(slug)
.use(reactRenderer)
.processSync(body, {
commonmark: true,
})
When doing a render I get the following snapshot using jest:
import renderer from 'react-test-renderer';
import remarkPlugins from "../remark-plugins.js"
it("should render markdown as react component", () => {
const md = remarkPlugins("## Test\n[link](href)").contents
const tree = renderer.create(md).toJSON();
expect(tree).toMatchSnapshot();
})
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`should render markdown as react component 1`] = `
<div>
<h2
id="user-content-test"
>
Test
</h2>
<p>
<a
href="href"
>
link
</a>
</p>
</div>
`;
As you can see, the "id" is longer than what I would expect. I tried adding remark-autolink-headings
in the first place: this one is actually adding href around "Test" with the correct slug (#test).
Do you have an idea about this problem and if it can be supported? (Without remark-slug I only get a simple h2, no id attribute.
Hi, I use VS Code to write markdown. And, I use an extension that adds section numbering to the headings like 1.1, 1.1.1 and so on.
For my website, I use Nuxt Content which uses remark-slug
plugin. So, the headings have their ID set as 1-1-something
. This causes an issue when I directly go the webpage with the hash, as the hash starts with number, and I think it's not allowed in CSS. So, the underlying Vue Router throws an error and the page doesn't load.
I can't stop using the VS Code extension which adds the automatic section numbering, because I work with huge markdown documents and I prefer to have heading numbering to quickly navigate inside VS Code's Outline sidebar. So, currently, I am unable to go the URL of the webpage using hash.
So, it would be great to simply add some -
or any other text or symbol before the IDs of the headings that start with number. This way, the IDs would not start with number (even if the Headings text start with number) and the Vue Router won't throw an error.
If in a mdx file I have something like
# This is my áccentuated title
The id generated keeps the accent
id="This-is-my-áccentuated-title"
But then, the link that you can recreate with "to="mywebpage#This-is-my-áccentuated-title"
doesn't go to the anchor because of the accent (at least what I'm seeing in Chrome)
Could it be possible to remove accents from strings?
remark-slug
currently just takes the text content of the heading and slugifies it for remark-autolink-headings
, I assume.
Unfortunately, I am on a nested URL like: /blog/contributing-to-open-source-projects-the-right-way
and not on the root path, therefore the a link redirects me to the root page instead of staying on the current page and jumping down.
Is there any solution or config option for that?
Jump to the anchor
It'd be helpful to be able to pass the slugger instance in as configuration.
In the context of a page with multiple chunks of markdown, it's possible for the same slug to be generated more than once on the same page.
If I anticipate that I'd like to be able to pass in an instance that the plugin would use to generate slugs rather than minting its own one. That way I can be in charge of resetting it when I decide it's necessary, and I can use the same slugger for multiple markdown renderings.
I suppose I could set a different clobberPrefix for each markdown field, but I'd rather not.
Given a headline appears multiple times within a set of markdown files that is being processed by unified-engine and remark-slug. Then resetting the slugger within the transformer produces slugs that are unique on a per-file basis.
As a remark-slug and unified-engine user I would like to be able to postprocess markdown files with pandoc and concatenate them into a single output file. In such a scenario I need slugs that are unique across the whole set of files and multiple syntax trees to maintain uniqueness after concatenation.
I propose an option multifile
which is false
by default for backwards compatibility but when true
prevents the slugger from being reset:
export default function remarkSlug(opts) {
const {multifile} = { multifile: false, ...opts }
slugs.reset() // reset once, only.
return (tree) => {
if (! multifile) {
slugs.reset()
}
// [...]
}
}
reset
, autoreset
, resetSlugs
Given the widespread use of remark and pandoc a contribution to remark-slug may be the best alternative.
when I upgrade remark-slug to version >=5.0.0
remark-autolink-headings is broken, but version 4.2.3 works.
TS definitions for the export would be much appreciated.
remark-slug does not add anchors to headings using GitHub’s algorithm.
This probably got to do with toString
from https://github.com/syntax-tree/mdast-util-to-string
N/A. Does not matter.
Example you have markdown
## <a name="foo"></a> Foo
## <a name="foo"></a> _Foo_
Use runkit for easy reproduction
https://runkit.com/endiliey/5dd56d8045205a0013aec93d
Slug should be like GitHub https://gist.github.com/endiliey/4516bbf630b0f34eeabe8900733cfbf6#-foo
-foo
and -foo-1
Slug is a-namefooa-foo
and a-namefooa-foo-1
I am trying to host the AWS docs on Next.js using MDX with the remark-slug plugin. The slug breaks when there is HTML in a header.
https://codesandbox.io/s/remark-debug-forked-9dggo?file=/src/index.js
I am expecting the slug to be consistent with how it is on GitHub.
I am getting id="sign-up-for-awsa-namesign-up-for-aws-gsga", but when viewed on GitHub the extension is #sign-up-for-aws.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.