From 35382c51917a30eb103f0b0c415904d8b8d3997d Mon Sep 17 00:00:00 2001 From: Shawn Thompson Date: Tue, 3 Sep 2024 13:20:33 -0400 Subject: [PATCH 1/7] capture URL of changed page and show in console --- .eleventy.js | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/.eleventy.js b/.eleventy.js index 4fb646c31..540a24592 100644 --- a/.eleventy.js +++ b/.eleventy.js @@ -1,3 +1,7 @@ +const fs = require('fs'); +const path = require('path'); +const { execSync } = require('child_process'); +const nunjucks = require('nunjucks'); // Import Nunjucks for rendering const markdownIt = require('markdown-it'); const markdownItAnchor = require('markdown-it-anchor'); const markdownItAttrs = require('markdown-it-attrs'); @@ -5,6 +9,11 @@ const { EleventyHtmlBasePlugin } = require('@11ty/eleventy'); const { stripHtml } = require('string-strip-html'); const beautify = require('js-beautify').html; const { DateTime } = require('luxon'); +const changedPageUrls = []; + +// ANSI escape codes for blue underline +const blueUnderline = '\x1b[4;34m'; // Underline and blue font +const resetColor = '\x1b[0m'; // Reset font to default module.exports = function (eleventyConfig) { const eleventySlugify = eleventyConfig.getFilter('slug'); @@ -133,6 +142,69 @@ module.exports = function (eleventyConfig) { return md.render(value); }); + let changedFilesMap = new Map(); + let changedFilePaths = new Set(); + let gitChangedUrls = []; + let domain = 'http://localhost'; + let port = '8080'; // Default port + + // Read the .eleventy-port file to get the correct port + if (fs.existsSync('.eleventy-port')) { + port = fs.readFileSync('.eleventy-port', 'utf8').trim(); + } + + // Capture changed files before the build starts (Method 1: Eleventy watch) + eleventyConfig.on('beforeWatch', (changedFiles) => { + changedFiles.forEach(file => { + console.log(`Changed source file: ${file}`); + changedFilesMap.set(file, null); // Track the file + }); + }); + + // Capture both committed and uncommitted changes using Git (Method 2: Git diff) + eleventyConfig.on('beforeBuild', () => { + const gitChangedFiles = execSync('git diff --name-only HEAD').toString().trim().split('\n'); + + gitChangedFiles.forEach((file) => { + // Check if the file is contained within the 'src/main' or 'src/pages' directories + // and is an .md or .njk file + if ((file.startsWith('src/main/') || file.startsWith('src/pages/')) && (file.endsWith('.md') || file.endsWith('.njk'))) { + // Track the file + changedFilePaths.add(file); + } + }); + }); + + // Hook into the HTML generation process (logging to the console) + eleventyConfig.addTransform("captureGeneratedUrl", function (content, outputPath) { + if (outputPath && outputPath.endsWith('.html')) { + const inputPath = path.relative('./', this.page.inputPath); + + // Check if this file was changed based on Git diff + if (changedFilePaths.has(inputPath)) { + const fullUrl = `${domain}:${port}${this.page.url}`; + + // Log the URL in blue and underlined + console.log(`${blueUnderline}Captured URL: ${fullUrl}${resetColor}`); + + gitChangedUrls.push(fullUrl); // Store the URL to log later + } + } + return content; + }); + + eleventyConfig.on('afterBuild', () => { + if (gitChangedUrls.length > 0) { + // Log the changed URLs at the end of the build + console.log('Changed file URLs:'); + gitChangedUrls.forEach(url => { + console.log(`${blueUnderline}${url}${resetColor}`); + }); + gitChangedUrls = []; // Clear the list for the next watch cycle + } + changedFilePaths.clear(); // Clear the set for the next watch cycle + }); + return { dir: { input: "src", From 2d2e339d3027ac2fd3fab8ac11933e144ec4a38e Mon Sep 17 00:00:00 2001 From: Shawn Thompson Date: Fri, 6 Sep 2024 14:45:41 -0400 Subject: [PATCH 2/7] create a page that list the pages instead of doing it in the console --- .eleventy.js | 45 ++++++++++++++++++++++----------- package.json | 6 ++--- src/main/en/pages-to-review.njk | 15 +++++++++++ src/main/fr/pages-a-reviser.njk | 15 +++++++++++ 4 files changed, 63 insertions(+), 18 deletions(-) create mode 100644 src/main/en/pages-to-review.njk create mode 100644 src/main/fr/pages-a-reviser.njk diff --git a/.eleventy.js b/.eleventy.js index 540a24592..1c35c3eb1 100644 --- a/.eleventy.js +++ b/.eleventy.js @@ -12,7 +12,7 @@ const { DateTime } = require('luxon'); const changedPageUrls = []; // ANSI escape codes for blue underline -const blueUnderline = '\x1b[4;34m'; // Underline and blue font +const underline = '\x1b[4m'; // Underline font const resetColor = '\x1b[0m'; // Reset font to default module.exports = function (eleventyConfig) { @@ -137,6 +137,19 @@ module.exports = function (eleventyConfig) { }); }); + // Custom collection for changed pages + eleventyConfig.addCollection('changedPages', function (collectionApi) { + const changedUrlsForTemplate = []; + collectionApi.getAll().forEach(item => { + const normalizedInputPath = path.relative('./', item.inputPath); + if (changedFilePaths.has(normalizedInputPath)) { + const fullUrl = `${domain}:${port}${item.url}`; + changedUrlsForTemplate.push(fullUrl); + } + }); + return changedUrlsForTemplate; // Returning the changed URLs for the template + }); + // Add custom Markdown filter for Nunjucks eleventyConfig.addNunjucksFilter("markdown", function (value) { return md.render(value); @@ -161,15 +174,14 @@ module.exports = function (eleventyConfig) { }); }); - // Capture both committed and uncommitted changes using Git (Method 2: Git diff) + // Capture both committed and uncommitted changes using Git eleventyConfig.on('beforeBuild', () => { const gitChangedFiles = execSync('git diff --name-only HEAD').toString().trim().split('\n'); gitChangedFiles.forEach((file) => { - // Check if the file is contained within the 'src/main' or 'src/pages' directories - // and is an .md or .njk file - if ((file.startsWith('src/main/') || file.startsWith('src/pages/')) && (file.endsWith('.md') || file.endsWith('.njk'))) { - // Track the file + // Track .md or .njk files in the 'src/main' or 'src/pages' directories + if ((file.startsWith('src/main/') || file.startsWith('src/pages/')) && + (file.endsWith('.md') || file.endsWith('.njk'))) { changedFilePaths.add(file); } }); @@ -185,7 +197,7 @@ module.exports = function (eleventyConfig) { const fullUrl = `${domain}:${port}${this.page.url}`; // Log the URL in blue and underlined - console.log(`${blueUnderline}Captured URL: ${fullUrl}${resetColor}`); + console.log(`${underline}Captured URL: ${fullUrl}${resetColor}`); gitChangedUrls.push(fullUrl); // Store the URL to log later } @@ -194,15 +206,18 @@ module.exports = function (eleventyConfig) { }); eleventyConfig.on('afterBuild', () => { - if (gitChangedUrls.length > 0) { - // Log the changed URLs at the end of the build - console.log('Changed file URLs:'); - gitChangedUrls.forEach(url => { - console.log(`${blueUnderline}${url}${resetColor}`); - }); - gitChangedUrls = []; // Clear the list for the next watch cycle + const changedFilesCount = changedFilePaths.size; + + if (changedFilesCount > 0) { + // Display summary and link to review page in the console + console.log(`\n${changedFilesCount} page(s) changed.`); + console.log(`Review the changed pages here: ${underline}${domain}:${port}/en/pages-to-review/${resetColor}\n`); + } else { + console.log('No pages to review.\n'); } - changedFilePaths.clear(); // Clear the set for the next watch cycle + + // Clear the set for the next watch cycle + changedFilePaths.clear(); }); return { diff --git a/package.json b/package.json index 5443cd4e4..5d54796c2 100644 --- a/package.json +++ b/package.json @@ -15,9 +15,9 @@ "sass-start": "sass src/_scss/base.scss _site/css/da11yn.css", "sass-build": "sass --style=compressed src/_scss/base.scss _site/css/da11yn.css", "watch:sass": "npm run sass-start -- --watch", - "start": "cross-env ELEVENTY_ENV=local npm-run-all sass-start serve:port --parallel watch:*", - "start-dev": "cross-env ELEVENTY_ENV=dev npm-run-all sass-start serve:port --parallel watch:*", - "start-prod": "cross-env ELEVENTY_ENV=prod npm-run-all sass-start serve:port --parallel watch:*", + "start": "cross-env ELEVENTY_WATCH=true ELEVENTY_ENV=local npm-run-all sass-start serve:port --parallel watch:*", + "start-dev": "cross-env ELEVENTY_WATCH=true ELEVENTY_ENV=dev npm-run-all sass-start serve:port --parallel watch:*", + "start-prod": "cross-env ELEVENTY_WATCH=true ELEVENTY_ENV=prod npm-run-all sass-start serve:port --parallel watch:*", "dev": "cross-env ELEVENTY_ENV=dev npm-run-all sass-build --parallel eleventy", "debug": "DEBUG=Eleventy* npx @11ty/eleventy", "build": "npm-run-all sass-build eleventy" diff --git a/src/main/en/pages-to-review.njk b/src/main/en/pages-to-review.njk new file mode 100644 index 000000000..e01d07cc0 --- /dev/null +++ b/src/main/en/pages-to-review.njk @@ -0,0 +1,15 @@ +--- +title: Pages to review +toggle: pages-a-reviser +--- + +{%- if collections.changedPages.length > 0 -%} +

These are changed pages that need to be reviewed:

+ +{%- else -%} +

There are no pages that have changes.

+{%- endif -%} diff --git a/src/main/fr/pages-a-reviser.njk b/src/main/fr/pages-a-reviser.njk new file mode 100644 index 000000000..7746b8d20 --- /dev/null +++ b/src/main/fr/pages-a-reviser.njk @@ -0,0 +1,15 @@ +--- +title: Pages à réviser +toggle: pages-to-review +--- + +{%- if collections.changedPages.length > 0 -%} +

Il s'agit de pages modifiées qui doivent être révisées :

+ +{%- else -%} +

Aucune page n'a été modifiée.

+{%- endif -%} From de13cec2b328301f4b520299ee83c0662a10e7be Mon Sep 17 00:00:00 2001 From: Shawn Thompson Date: Mon, 9 Sep 2024 12:32:45 -0400 Subject: [PATCH 3/7] add domain varibles depending on the environment --- .eleventy.js | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/.eleventy.js b/.eleventy.js index 1c35c3eb1..360b4b557 100644 --- a/.eleventy.js +++ b/.eleventy.js @@ -143,11 +143,11 @@ module.exports = function (eleventyConfig) { collectionApi.getAll().forEach(item => { const normalizedInputPath = path.relative('./', item.inputPath); if (changedFilePaths.has(normalizedInputPath)) { - const fullUrl = `${domain}:${port}${item.url}`; + const fullUrl = `${item.url}`; // Use relative URLs changedUrlsForTemplate.push(fullUrl); } }); - return changedUrlsForTemplate; // Returning the changed URLs for the template + return changedUrlsForTemplate; // Return changed URLs for the template }); // Add custom Markdown filter for Nunjucks @@ -161,9 +161,23 @@ module.exports = function (eleventyConfig) { let domain = 'http://localhost'; let port = '8080'; // Default port - // Read the .eleventy-port file to get the correct port + // Detect GitHub Codespaces + if (process.env.CODESPACES && process.env.CODESPACE_NAME) { + // Use the correct format for Codespaces URLs with the .app domain + domain = `https://${process.env.CODESPACE_NAME}-${port}.app.github.dev`; + } + + // Detect Netlify Deploy Preview + if (process.env.DEPLOY_URL) { + domain = process.env.DEPLOY_URL; + } + + // Read the .eleventy-port file to get the correct port for local development if (fs.existsSync('.eleventy-port')) { port = fs.readFileSync('.eleventy-port', 'utf8').trim(); + if (domain === 'http://localhost') { + domain = `${domain}:${port}`; // Apply the port for localhost + } } // Capture changed files before the build starts (Method 1: Eleventy watch) @@ -209,15 +223,14 @@ module.exports = function (eleventyConfig) { const changedFilesCount = changedFilePaths.size; if (changedFilesCount > 0) { - // Display summary and link to review page in the console + // Log summary and provide the correct link based on the environment console.log(`\n${changedFilesCount} page(s) changed.`); - console.log(`Review the changed pages here: ${underline}${domain}:${port}/en/pages-to-review/${resetColor}\n`); + console.log(`Review the changed pages here: ${underline}${domain}/en/pages-to-review/${resetColor}\n`); } else { console.log('No pages to review.\n'); } - // Clear the set for the next watch cycle - changedFilePaths.clear(); + changedFilePaths.clear(); // Clear the set for the next watch cycle }); return { From 0e6391bf1c86ea1959e29500480ae7ad65cfc087 Mon Sep 17 00:00:00 2001 From: Shawn Thompson Date: Mon, 9 Sep 2024 19:40:24 +0000 Subject: [PATCH 4/7] add title to review page as link text --- .eleventy.js | 11 +++++++---- src/main/en/pages-to-review.njk | 14 +++++++------- src/main/fr/pages-a-reviser.njk | 2 +- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/.eleventy.js b/.eleventy.js index 360b4b557..811e11f32 100644 --- a/.eleventy.js +++ b/.eleventy.js @@ -139,15 +139,18 @@ module.exports = function (eleventyConfig) { // Custom collection for changed pages eleventyConfig.addCollection('changedPages', function (collectionApi) { - const changedUrlsForTemplate = []; + const changedPages = []; collectionApi.getAll().forEach(item => { const normalizedInputPath = path.relative('./', item.inputPath); if (changedFilePaths.has(normalizedInputPath)) { - const fullUrl = `${item.url}`; // Use relative URLs - changedUrlsForTemplate.push(fullUrl); + // Store both the URL and title for each changed page + changedPages.push({ + url: item.url, + title: item.data.title || item.fileSlug // fallback to fileSlug if no title + }); } }); - return changedUrlsForTemplate; // Return changed URLs for the template + return changedPages; // Returning the changed URLs and titles for the template }); // Add custom Markdown filter for Nunjucks diff --git a/src/main/en/pages-to-review.njk b/src/main/en/pages-to-review.njk index e01d07cc0..5ba63ea8c 100644 --- a/src/main/en/pages-to-review.njk +++ b/src/main/en/pages-to-review.njk @@ -4,12 +4,12 @@ toggle: pages-a-reviser --- {%- if collections.changedPages.length > 0 -%} -

These are changed pages that need to be reviewed:

-
    - {% for page in collections.changedPages %} -
  • {{ page }}
  • - {% endfor %} -
+

These are changed pages that need to be reviewed:

+
    + {% for page in collections.changedPages %} +
  • {{ page.title }}
  • + {% endfor %} +
{%- else -%} -

There are no pages that have changes.

+

There are no pages that have changes.

{%- endif -%} diff --git a/src/main/fr/pages-a-reviser.njk b/src/main/fr/pages-a-reviser.njk index 7746b8d20..3a44b8eea 100644 --- a/src/main/fr/pages-a-reviser.njk +++ b/src/main/fr/pages-a-reviser.njk @@ -7,7 +7,7 @@ toggle: pages-to-review

Il s'agit de pages modifiées qui doivent être révisées :

{%- else -%} From 16419d6465fcaba3dbfbd9b8ee23e18958ad22e8 Mon Sep 17 00:00:00 2001 From: Shawn Thompson Date: Mon, 9 Sep 2024 20:28:09 +0000 Subject: [PATCH 5/7] add lang attributes to links --- .eleventy.js | 7 ++++--- src/main/en/pages-to-review.njk | 18 +++++++++++------- src/main/fr/pages-a-reviser.njk | 11 ++++++++--- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/.eleventy.js b/.eleventy.js index 811e11f32..f08b4d033 100644 --- a/.eleventy.js +++ b/.eleventy.js @@ -143,14 +143,15 @@ module.exports = function (eleventyConfig) { collectionApi.getAll().forEach(item => { const normalizedInputPath = path.relative('./', item.inputPath); if (changedFilePaths.has(normalizedInputPath)) { - // Store both the URL and title for each changed page + // Store both the URL, title, and locale for each changed page changedPages.push({ url: item.url, - title: item.data.title || item.fileSlug // fallback to fileSlug if no title + title: item.data.title || item.fileSlug, // fallback to fileSlug if no title + locale: item.data.locale || 'en' // default to 'en' if no locale is set }); } }); - return changedPages; // Returning the changed URLs and titles for the template + return changedPages; // Returning the changed URLs, titles, and locales for the template }); // Add custom Markdown filter for Nunjucks diff --git a/src/main/en/pages-to-review.njk b/src/main/en/pages-to-review.njk index 5ba63ea8c..68aaec8cd 100644 --- a/src/main/en/pages-to-review.njk +++ b/src/main/en/pages-to-review.njk @@ -4,12 +4,16 @@ toggle: pages-a-reviser --- {%- if collections.changedPages.length > 0 -%} -

These are changed pages that need to be reviewed:

-
    - {% for page in collections.changedPages %} -
  • {{ page.title }}
  • - {% endfor %} -
+

These are changed pages that need to be reviewed:

+
    + {% for page in collections.changedPages %} + {% if page.locale != locale %} +
  • {{ page.title }}
  • + {% else %} +
  • {{ page.title }}
  • + {% endif %} + {% endfor %} +
{%- else -%} -

There are no pages that have changes.

+

There are no pages that have changes.

{%- endif -%} diff --git a/src/main/fr/pages-a-reviser.njk b/src/main/fr/pages-a-reviser.njk index 3a44b8eea..bc10beab4 100644 --- a/src/main/fr/pages-a-reviser.njk +++ b/src/main/fr/pages-a-reviser.njk @@ -6,9 +6,14 @@ toggle: pages-to-review {%- if collections.changedPages.length > 0 -%}

Il s'agit de pages modifiées qui doivent être révisées :

    - {% for page in collections.changedPages %} -
  • {{ page.title }}
  • - {% endfor %} + {% for page in collections.changedPages %} + {% if page.locale != locale %} +
  • {{ page.title }}
  • + {% else %} +
  • {{ page.title }}
  • + {% endif %} + {% endfor %} +
{%- else -%}

Aucune page n'a été modifiée.

From e3b8956dfb358fb267dc8e7e9130ffbaa70d9ca3 Mon Sep 17 00:00:00 2001 From: Shawn Thompson Date: Mon, 9 Sep 2024 20:59:29 +0000 Subject: [PATCH 6/7] changed git diff to compare to main branch instead of HEAD --- .eleventy.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/.eleventy.js b/.eleventy.js index f08b4d033..3afdd2f52 100644 --- a/.eleventy.js +++ b/.eleventy.js @@ -192,14 +192,25 @@ module.exports = function (eleventyConfig) { }); }); - // Capture both committed and uncommitted changes using Git + // Capture both committed and uncommitted changes using Git (Method 2: Git diff) eleventyConfig.on('beforeBuild', () => { - const gitChangedFiles = execSync('git diff --name-only HEAD').toString().trim().split('\n'); + // Fetch the `main` branch from the upstream repository directly + const upstreamUrl = 'https://github.com/gc-da11yn/gc-da11yn.github.io'; + + try { + execSync(`git fetch ${upstreamUrl} main:upstream-main --depth=1`); + } catch (err) { + console.error('Error fetching the upstream main branch', err); + } + + // Get the diff between the current branch and `upstream-main` + const gitChangedFiles = execSync('git diff upstream-main --name-only').toString().trim().split('\n'); gitChangedFiles.forEach((file) => { - // Track .md or .njk files in the 'src/main' or 'src/pages' directories - if ((file.startsWith('src/main/') || file.startsWith('src/pages/')) && - (file.endsWith('.md') || file.endsWith('.njk'))) { + // Check if the file is contained within the 'src/main' or 'src/pages' directories + // and is an .md or .njk file + if ((file.startsWith('src/main/') || file.startsWith('src/pages/')) && (file.endsWith('.md') || file.endsWith('.njk'))) { + // Track the file changedFilePaths.add(file); } }); From 844e5938cd49dd08ec52fc32edd93d3905fa0982 Mon Sep 17 00:00:00 2001 From: Shawn Thompson Date: Tue, 10 Sep 2024 12:33:22 -0400 Subject: [PATCH 7/7] add description and notice on review page --- src/main/en/pages-to-review.njk | 3 +++ src/main/fr/pages-a-reviser.njk | 3 +++ 2 files changed, 6 insertions(+) diff --git a/src/main/en/pages-to-review.njk b/src/main/en/pages-to-review.njk index 68aaec8cd..87583708e 100644 --- a/src/main/en/pages-to-review.njk +++ b/src/main/en/pages-to-review.njk @@ -1,6 +1,7 @@ --- title: Pages to review toggle: pages-a-reviser +description: On this page, you’ll find a list of pages that have been modified and require review. These changes reflect the differences between the current branch and the main branch. --- {%- if collections.changedPages.length > 0 -%} @@ -17,3 +18,5 @@ toggle: pages-a-reviser {%- else -%}

There are no pages that have changes.

{%- endif -%} + +

If you see links to pages you haven't changed, it may be because there were changes on the upstream / main branch. To update your current branch to the latest, use git rebase.

diff --git a/src/main/fr/pages-a-reviser.njk b/src/main/fr/pages-a-reviser.njk index bc10beab4..b6f497f01 100644 --- a/src/main/fr/pages-a-reviser.njk +++ b/src/main/fr/pages-a-reviser.njk @@ -1,6 +1,7 @@ --- title: Pages à réviser toggle: pages-to-review +description: Sur cette page, vous trouverez une liste des pages qui ont été modifiées et qui nécessitent une révision. Ces modifications reflètent les différences entre la branche actuelle et la branche principale. --- {%- if collections.changedPages.length > 0 -%} @@ -18,3 +19,5 @@ toggle: pages-to-review {%- else -%}

Aucune page n'a été modifiée.

{%- endif -%} + +

Si vous voyez des liens vers des pages que vous n'avez pas modifiées, c'est peut-être parce qu'il y a eu des changements sur la branche upstream / main. Pour mettre à jour votre branche actuelle avec les dernières modifications, utilisez git rebase.