This repository has been archived by the owner on Feb 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MWPW-144868 Card Metadata Refinement
- Loading branch information
Showing
11 changed files
with
201 additions
and
45 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { visitParents } from 'unist-util-visit-parents'; | ||
import { select } from 'unist-util-select'; | ||
import { getBlockInfo } from '../migration-tools/select.js'; | ||
|
||
/** | ||
* Maps the ancestors array and returns an array of ancestor types. | ||
* If the ancestor type is 'gridTable', it finds the first text node in the table | ||
* and extracts the block variant from it. | ||
* | ||
* @param {Array} ancestors - The array of ancestors to map. | ||
* @returns {Array} - The array of mapped ancestor types. | ||
*/ | ||
export const mapAncestors = (ancestors) => ancestors.map((ancestor) => { | ||
if (ancestor.type !== 'gridTable') { | ||
return ancestor.type; | ||
} | ||
// find the first text node in the table | ||
const cell = select('text', ancestor); | ||
const { variant } = getBlockInfo(cell.value); | ||
|
||
return `${ancestor.type} '${variant}'`; | ||
}); | ||
|
||
export default function validateMdast(mdast) { | ||
visitParents(mdast, 'text', (node, ancestors) => { | ||
if (!node.value) { | ||
const structure = `${mapAncestors(ancestors).join(' > ')} > ${node.type}`; | ||
throw new Error(`Invalid text node ${structure}: ${JSON.stringify(node)}`); | ||
} | ||
}); | ||
} |
This file contains 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 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 |
---|---|---|
@@ -1,45 +1,74 @@ | ||
import ExcelReporter from '../bulk-update/reporter/excel-reporter.js'; | ||
import { saveDocument } from '../bulk-update/document-manager/document-manager.js'; | ||
/** | ||
* Example Migration using the bulk-update library. | ||
*/ | ||
|
||
import { BulkUpdate, ExcelReporter, saveDocument } from '../bulk-update/index.js'; | ||
|
||
const { pathname } = new URL('.', import.meta.url); | ||
const config = { | ||
list: ['/'], // The list of entries to migrate | ||
siteUrl: 'https://main--bacom--adobecom.hlx.live', // The site URL | ||
reporter: new ExcelReporter(`${pathname}reports/example.xlsx`), // The logging type | ||
outputDir: `${pathname}output`, // The output directory for the docx files | ||
list: ['/'], // The list of entries to migrate. | ||
siteUrl: 'https://main--bacom--adobecom.hlx.live', // The site URL. | ||
reporter: new ExcelReporter(`${pathname}reports/example.xlsx`, true), // The logging type, save location and autosave. | ||
outputDir: `${pathname}output`, // The output directory for the docx files. | ||
mdDir: `${pathname}md`, // The directory for storing the fetched markdown. | ||
mdCacheMs: 0, // The markdown cache time in milliseconds. | ||
}; | ||
|
||
/** | ||
* Example Migration, run using `npm run bulk-update 'migration-example'` | ||
* Adds a "Hello World" heading to the given mdast. | ||
* | ||
* @param {Object} mdast - The mdast object to modify. | ||
*/ | ||
function addHelloWorld(mdast, entry) { | ||
const helloWorld = { | ||
type: 'heading', | ||
depth: 1, | ||
children: [ | ||
{ | ||
type: 'text', | ||
value: 'Hello World', | ||
}, | ||
], | ||
}; | ||
|
||
mdast.children.unshift(helloWorld); | ||
|
||
// Log the migration to the hello world tab with a status of success. | ||
config.reporter.log('hello world', 'success', 'Added Hello World', { entry }); | ||
} | ||
|
||
/** | ||
* Run using `npm run bulk-update 'migration-example'` | ||
* | ||
* @returns {Object} - The configuration object for the migration. | ||
*/ | ||
export function init() { | ||
// Any file path filtering of the list can be done here. | ||
return config; | ||
} | ||
|
||
/** | ||
* Example Migration | ||
* | ||
* @param {Object} document - The document to be migrated. | ||
* @param {string} document.entry - The entry path of the document. | ||
* @param {Object} document.mdast - The Markdown AST of the document. | ||
*/ | ||
export async function migrate(document) { | ||
const { mdast } = document; | ||
const { mdast, entry } = document; | ||
// Additional filtering base on content can be done here. | ||
|
||
mdast.children.unshift({ | ||
type: 'heading', | ||
depth: 1, | ||
children: [ | ||
{ | ||
type: 'text', | ||
value: 'Hello World', | ||
}, | ||
], | ||
}); | ||
// Helper functions, add a heading to the document. | ||
addHelloWorld(mdast, entry); | ||
|
||
config.reporter.log('hello world', 'success', 'Added Hello World', document.entry); | ||
// Save the document after migrating. | ||
await saveDocument(document, config); | ||
} | ||
|
||
/** | ||
* Run using `node migration-example/custom-migration.js` | ||
*/ | ||
if (import.meta.url === `file://${process.argv[1]}`) { | ||
await BulkUpdate(config, migrate); | ||
process.exit(); | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Hello World Migration | ||
|
||
## Description | ||
|
||
This migration adds a "hello world" header to the top of the document. | ||
This is an example of using the bulk update library located at `bulk-update/index.js` to perform bulk updates. | ||
|
||
## Usage | ||
|
||
Run the migration script using the command `npm run bulk-update 'migration-example'` or `node migration-example/custom-migration.js`. | ||
|
||
## Overview | ||
|
||
The `migration-example/migration.js` script is responsible for setting up the configuration object and performing the actual migration. | ||
The `init` function returns the configuration object for the migration, and the `migrate` function performs the actual migration. | ||
|
||
## Custom Migrations | ||
|
||
For complete control over the migration process, you can create a custom migration script. The `migration-example/custom-migration.js` script is an example of this. |
This file contains 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
Oops, something went wrong.