feat(aio): add GH-style anchor links to headings (#16161)

Closes #16126

PR Close #16161
This commit is contained in:
Peter Bacon Darwin
2017-04-19 16:03:15 +01:00
committed by Miško Hevery
parent 1bfa7c6f14
commit 1e848d696b
8 changed files with 94 additions and 7 deletions

View File

@ -12,7 +12,7 @@ module.exports = function fixInternalDocumentLinks() {
return {
$runAfter: ['inlineTagProcessor'],
$runBefore: ['writeFilesProcessor'],
$runBefore: ['convertToJsonProcessor'],
$process: function(docs) {
docs.forEach(doc => {
doc.renderedContent = doc.renderedContent.replace(INTERNAL_LINK, (_, pre, hash) => {

View File

@ -13,7 +13,7 @@ describe('fixInternalDocumentLinks processor', () => {
it('should run before the correct processor', () => {
const processor = processorFactory();
expect(processor.$runBefore).toEqual(['writeFilesProcessor']);
expect(processor.$runBefore).toEqual(['convertToJsonProcessor']);
});
it('should run after the correct processor', () => {

View File

@ -1,4 +1,6 @@
const remark = require('remark');
const slug = require('remark-slug');
const autolinkHeadings = require('remark-autolink-headings');
const html = require('remark-html');
/**
@ -15,6 +17,8 @@ module.exports = function renderMarkdown() {
// .use(() => tree => {
// console.log(require('util').inspect(tree, { colors: true, depth: 4 }));
// })
.use(slug)
.use(autolinkHeadings)
.use(html);
return function renderMarkdownImpl(content) {

View File

@ -16,7 +16,7 @@ describe('remark: renderMarkdown service', () => {
const output = renderMarkdown(content);
expect(output).toEqual(
'<h1>heading 1</h1>\n' +
'<h1 id="heading-1"><a href="#heading-1" aria-hidden="true"><span class="icon icon-link"></span></a>heading 1</h1>\n' +
'<p>A paragraph with <strong>bold</strong> and <em>italic</em>.</p>\n' +
'<ul>\n' +
'<li>List item 1</li>\n' +
@ -25,9 +25,9 @@ describe('remark: renderMarkdown service', () => {
});
it('should not process markdown inside inline tags', () => {
const content = '# heading {@link some_url_path}';
const content = '* list item {@link some_url_path}';
const output = renderMarkdown(content);
expect(output).toEqual('<h1>heading {@link some_url_path}</h1>\n');
expect(output).toEqual('<ul>\n<li>list item {@link some_url_path}</li>\n</ul>\n');
});
it('should not put block level inline tags inside paragraphs', () => {
@ -67,4 +67,14 @@ describe('remark: renderMarkdown service', () => {
const output = renderMarkdown(content);
expect(output).toEqual('<p>some text</p>\n<p> indented text</p>\n<p>other text</p>\n');
});
it('should add id slugs and links to headings', () => {
const content = '# heading 1\n\nSome text\n\n## heading 2\n\nMore text';
const output = renderMarkdown(content);
expect(output).toEqual(
'<h1 id="heading-1"><a href="#heading-1" aria-hidden="true"><span class="icon icon-link"></span></a>heading 1</h1>\n' +
'<p>Some text</p>\n' +
'<h2 id="heading-2"><a href="#heading-2" aria-hidden="true"><span class="icon icon-link"></span></a>heading 2</h2>\n' +
'<p>More text</p>\n');
});
});