Skip to content

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 3 commits into from
Mar 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"gulp-clean-css": "^3.0.3",
"gulp-cli": "^1.2.2",
"gulp-connect": "^5.0.0",
"gulp-dom": "^0.9.17",
"gulp-flatten": "^0.3.1",
"gulp-highlight-files": "0.0.4",
"gulp-htmlmin": "^3.0.0",
Expand Down
47 changes: 43 additions & 4 deletions tools/gulp/tasks/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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', () => {
Expand All @@ -38,6 +59,7 @@ task('markdown-docs', () => {
}
}))
.pipe(transform(transformMarkdownFiles))
.pipe(dom(createTagNameAliaser('docs-markdown')))
.pipe(dest('dist/docs/markdown'));
});

Expand All @@ -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', () => {
Expand Down Expand Up @@ -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() {
Copy link
Member

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

Copy link
Contributor Author

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

MARKDOWN_TAGS_TO_CLASS_ALIAS.forEach(tag => {
for (let el of this.querySelectorAll(tag)) {
el.classList.add(`${classPrefix}-${tag}`);
}
});

return this;
};
}