Compare commits

...
Sign in to create a new pull request.

2 commits

2 changed files with 58 additions and 23 deletions

View file

@ -1,31 +1,56 @@
String.prototype.replaceLast = function (what, replacement) {
var pcs = this.split(what);
var lastPc = pcs.pop();
return pcs.join(what) + replacement + lastPc;
}
async function getContent(url) {
const res = await fetch(url.concat('/download'));
return res.text();
}
async function migrateDocument(url, baseUrl) {
const content = await getContent(url);
cy.request({
url: baseUrl.concat('/new'),
method: 'POST',
headers: {
'Content-Type': 'text/markdown',
'Access-Control-Allow-Origin': new URL(baseUrl).hostname,
},
body: content,
}).then((res) => {
const redirect = res.redirects[0].split(' ')[1];
cy.visit(url);
cy.get('#view-mode-toggle-edit').click({force: true});
cy.get('.CodeMirror-scroll').type('{ctrl}a{backspace}');
cy.get('.CodeMirror-scroll').type(`Moved to [${redirect}](${redirect})`);
});
async function migrateDocument(oldUrl, newUrl) {
// get content of old pad
var content = await getContent(oldUrl);
// replace URLs
content = content.replaceAll(new URL(oldUrl).hostname, new URL(newUrl).hostname);
// visit new pad url (not possible via post api request because pad may already exists)
// Caution: Content of new pad url will be overwritten!
// Caution: History will not be moved to new pad url
cy.visit(newUrl.concat('?edit'));
cy.window().then((win) => {
// Write Content
cy.get('.CodeMirror-scroll').type('{ctrl}a{backspace}');
cy.get('.CodeMirror-scroll').type(content);
// Visit old pad and replace content
cy.visit(oldUrl);
cy.get('#view-mode-toggle-edit').click({force: true});
cy.get('.CodeMirror-scroll').type('{ctrl}a{backspace}');
cy.get('.CodeMirror-scroll').type(`# 301 Pad moved{enter}=> [${newUrl}](${newUrl})`);
})
}
describe('Migrate document', () => {
it('passes', async () => {
const baseUrl = 'https://md.margau.net';
const url = 'https://md.margau.net/H0JO3L5DS-6Yhv4RrdS-tw';
migrateDocument(url, baseUrl);
});
});
it('passes', async () => {
// Read list of pads to migrate
cy.fixture('pads').then(async(pads) => {
if(pads.length === 0) {
console.log("Didn't find any pad urls to migrate");
}
for (const pad of pads) {
await migrateDocument(pad.oldUrl, pad.newUrl);
}
})
})
})

View file

@ -0,0 +1,10 @@
[
{
"oldUrl":"https://md.margau.net/test",
"newUrl":"https://pad.hacknang.de/test"
},
{
"oldUrl":"https://md.margau.net/test3",
"newUrl":"https://pad.hacknang.de/test2"
}
]