-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: backend folder to use to server process * fix: remove www.azion.com/en redirect to www.azion.com/en/ * wip: saving to generate a commit with the redirects update * fix: removing from homes the 30x stasus redirect * wip: replace full url using semrush pdf * fix: replace links 30x * fix: permalinks 30x * fix: space link attribute
- Loading branch information
1 parent
feb57e5
commit e1e5689
Showing
47 changed files
with
15,199 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { parse } from 'csv-parse' | ||
import fs from 'fs' | ||
import { createReadStream } from 'fs' | ||
import path from 'path' | ||
import matter from 'gray-matter' | ||
|
||
|
||
let counterFoundLinks = 0 | ||
let PATH = { | ||
csv: './www.azion.com_permanent_redirects_20241225.csv', | ||
docs: `${process.env.OLDPWD}/src/content/docs` | ||
} | ||
const wwwazioncom = 'https://www.azion.com' | ||
|
||
function findReplace(content, oldUrl, newUrl) { | ||
return content.replace(oldUrl, newUrl) | ||
} | ||
|
||
async function loadRedirects() { | ||
const redirects = [] | ||
const parser = createReadStream(PATH.csv).pipe( | ||
parse({ | ||
columns: true, | ||
skip_empty_lines: true, | ||
}) | ||
) | ||
|
||
for await (const record of parser) { | ||
redirects.push({ | ||
page: record.page, | ||
initialUrl: record.initial_url, | ||
destinationUrl: record.destination_url, | ||
statusCode: record.status, | ||
discovered: record.discovered | ||
}) | ||
} | ||
|
||
return redirects | ||
} | ||
|
||
|
||
async function processFile(filePath, redirects) { | ||
fs.readFile(filePath, async (err, content) => { | ||
if(err) { | ||
console.error(err) | ||
return | ||
} | ||
|
||
const { data, content: markdownContent } = matter(content) | ||
const utf8Content = Buffer.from(content).toString('utf-8') | ||
|
||
for (const item of redirects) { | ||
const pagePermalink = item.page.replace(wwwazioncom, '').replace('/pt-br', '').replace('/en', '') | ||
const url30x = item.initialUrl === wwwazioncom ? wwwazioncom : item.initialUrl | ||
const url200 = item.destinationUrl | ||
const isRoot = url30x === wwwazioncom | ||
const rgx = new RegExp(`\\(${url30x}\\)`, 'g') | ||
const contentMatch = utf8Content.match(rgx) | ||
|
||
if(!contentMatch) continue | ||
counterFoundLinks++ | ||
|
||
console.log(`{ | ||
isRoot: ${isRoot}, | ||
pagePermalink: ${pagePermalink}, | ||
file: ${filePath}, | ||
permalink: ${data.permalink}, | ||
rgx: ${rgx}, | ||
url30x: ${url30x}, | ||
url200: ${url200}, | ||
contentMatch: ${contentMatch}, | ||
contentMatchCount: ${contentMatch.length}, | ||
processedCount: ${counterFoundLinks} | ||
}`) | ||
|
||
const newContent = findReplace(utf8Content, isRoot ? /\\(https\:\/\/www\.azion\.com\/\\)/ : rgx, `(${url200})`) | ||
await fs.writeFile(filePath, newContent, async (err) => { | ||
if(err) throw err | ||
console.log(`[OK] ${filePath} updated`) | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
function processDirectory(directory, redirects) { | ||
fs.readdir(directory, { withFileTypes: true }, (err, entries) => { | ||
if (err) { | ||
console.error('[ERROR] directory can not be readed:', err) | ||
return | ||
} | ||
|
||
for (const entry of entries) { | ||
const fullPath = path.join(directory, entry.name) | ||
|
||
if(entry.isDirectory()) { | ||
processDirectory(fullPath, redirects) | ||
} else if (entry.isFile()) { | ||
processFile(fullPath, redirects) | ||
} else { | ||
console.error(`[ERROR] ${fullPath} is not a file or directory`) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
async function main() { | ||
try { | ||
const redirects = await loadRedirects() | ||
processDirectory(PATH.docs, redirects) | ||
} catch (error) { | ||
console.error('[ERROR] ', error) | ||
process.exit(1) | ||
} | ||
} | ||
|
||
main() |
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,16 @@ | ||
{ | ||
"name": "semrush-replace-url", | ||
"version": "0.0.1", | ||
"main": "index.js", | ||
"type": "module", | ||
"scripts": { | ||
"start": "node index.js" | ||
}, | ||
"author": "[email protected]", | ||
"license": "MIT", | ||
"description": "", | ||
"dependencies": { | ||
"csv-parse": "^5.6.0", | ||
"gray-matter": "^4.0.3" | ||
} | ||
} |
15,022 changes: 15,022 additions & 0 deletions
15,022
backend/semrush-replace-url/www.azion.com_permanent_redirects_20241225.csv
Large diffs are not rendered by default.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
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
Oops, something went wrong.