-
Notifications
You must be signed in to change notification settings - Fork 6.8k
class alias tags in html generated from markdown #3525
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ const highlight = require('gulp-highlight-files'); | |
const rename = require('gulp-rename'); | ||
const flatten = require('gulp-flatten'); | ||
const hljs = require('highlight.js'); | ||
const dom = require('gulp-dom'); | ||
|
||
// Our docs contain comments of the form `<!-- example(...) -->` which serve as placeholders where | ||
// example code should be inserted. We replace these comments with divs that have a | ||
|
@@ -21,6 +22,26 @@ const EXAMPLE_PATTERN = /<!--\W*example\(([^)]+)\)\W*-->/g; | |
// documentation page. Using a RegExp to rewrite links in HTML files to work in the docs. | ||
const LINK_PATTERN = /(<a[^>]*) href="([^"]*)"/g; | ||
|
||
// HTML tags in the markdown generated files that should receive a .docs-markdown-${tagName} class | ||
// for styling purposes. | ||
const MARKDOWN_TAGS_TO_CLASS_ALIAS = [ | ||
'a', | ||
'h1', | ||
'h2', | ||
'h3', | ||
'h4', | ||
'h5', | ||
'li', | ||
'ol', | ||
'p', | ||
'table', | ||
'tbody', | ||
'td', | ||
'th', | ||
'tr', | ||
'ul' | ||
]; | ||
|
||
task('docs', ['markdown-docs', 'highlight-docs', 'api-docs']); | ||
|
||
task('markdown-docs', () => { | ||
|
@@ -38,6 +59,7 @@ task('markdown-docs', () => { | |
} | ||
})) | ||
.pipe(transform(transformMarkdownFiles)) | ||
.pipe(dom(createTagNameAliaser('docs-markdown'))) | ||
.pipe(dest('dist/docs/markdown')); | ||
}); | ||
|
||
|
@@ -49,10 +71,10 @@ task('highlight-docs', () => { | |
}; | ||
|
||
return src('src/examples/**/*.+(html|css|ts)') | ||
.pipe(flatten()) | ||
.pipe(rename(renameFile)) | ||
.pipe(highlight()) | ||
.pipe(dest('dist/docs/examples')); | ||
.pipe(flatten()) | ||
.pipe(rename(renameFile)) | ||
.pipe(highlight()) | ||
.pipe(dest('dist/docs/examples')); | ||
}); | ||
|
||
task('api-docs', () => { | ||
|
@@ -95,3 +117,20 @@ function fixMarkdownDocLinks(link: string, filePath: string): string { | |
// guides can be loaded in the Material docs. | ||
return `guide/${baseName}`; | ||
} | ||
|
||
/** | ||
* Returns a function to be called with an HTML document as its context that aliases HTML tags by | ||
* adding a class consisting of a prefix + the tag name. | ||
* @param classPrefix The prefix to use for the alias class. | ||
*/ | ||
function createTagNameAliaser(classPrefix: string) { | ||
return function() { | ||
MARKDOWN_TAGS_TO_CLASS_ALIAS.forEach((tag) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: parens aren't needed |
||
for (let el of this.querySelectorAll(tag)) { | ||
el.classList.add(`${classPrefix}-${tag}`); | ||
} | ||
}); | ||
|
||
return this; | ||
}; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: can be an arrow function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It actually won't get the correct context if I use an arrow fn