All files gulp.seo.ts

25.45% Statements 14/55
100% Branches 0/0
0% Functions 0/1
25.45% Lines 14/55

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 561x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                               1x 1x     1x  
import ansiColors from 'ansi-colors';
import gulp from 'gulp';
import gulpDom from 'gulp-dom';
import gulpCached from './gulp-utils/gulp.cache';
import Logger from './utils/logger';
import { commonIgnore, deployConfig, getConfig } from './_config';
 
/**
 * Auto seo runner
 * @param cwd working directory to scan html's
 */
export function taskSeo(_done: gulp.TaskFunctionCallback | null | undefined, cwd: string) {
  const config = getConfig();
  const ignore: string[] = Array.isArray(config.exclude) ? config.exclude : [];
  ignore.push(...commonIgnore);
  return gulp
    .src(['**/*.{htm,html}', '*.{html,htm}'], { cwd, ignore })
    .pipe(gulpCached({ name: 'seo' }))
    .pipe(
      gulpDom(function (path) {
        // fix alt images
        const images = Array.from(this.querySelectorAll('img[src]'));
        images.forEach((el) => {
          const alt = el.getAttribute('alt');
          if (!alt || alt.length === 0) {
            const title = this.title + ' - ' + el.getAttribute('src') || 'No Alt';
            el.setAttribute('alt', title);
          }
        });

        // fix title iframe
        const iframes = Array.from(this.querySelectorAll('iframe[src]'));
        iframes.forEach((el) => {
          const alt = el.getAttribute('title');
          if (!alt || alt.length === 0) {
            const title = this.title + ' - ' + el.getAttribute('src') || 'No Title';
            el.setAttribute('title', title);
          }
        });

        // WARNING MAKER
        // count H1
        const h1 = this.querySelectorAll('h1');
        if (h1.length > 1) {
          Logger.log(ansiColors.yellowBright('[WARN]'), `H1 (${h1.length}) ${path}`);
        }
      })
    )
    .pipe(gulp.dest(cwd));
}
 
gulp.task('seo', function () {
  const { deployDir } = deployConfig();
  return taskSeo(null, deployDir);
});