Compare commits
2 commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
d10cf37bfd | ||
![]() |
2b30b277e7 |
2 changed files with 58 additions and 23 deletions
|
@ -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) {
|
async function getContent(url) {
|
||||||
const res = await fetch(url.concat('/download'));
|
const res = await fetch(url.concat('/download'));
|
||||||
return res.text();
|
return res.text();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function migrateDocument(url, baseUrl) {
|
async function migrateDocument(oldUrl, newUrl) {
|
||||||
const content = await getContent(url);
|
// get content of old pad
|
||||||
cy.request({
|
var content = await getContent(oldUrl);
|
||||||
url: baseUrl.concat('/new'),
|
|
||||||
method: 'POST',
|
// replace URLs
|
||||||
headers: {
|
content = content.replaceAll(new URL(oldUrl).hostname, new URL(newUrl).hostname);
|
||||||
'Content-Type': 'text/markdown',
|
|
||||||
'Access-Control-Allow-Origin': new URL(baseUrl).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!
|
||||||
body: content,
|
// Caution: History will not be moved to new pad url
|
||||||
}).then((res) => {
|
cy.visit(newUrl.concat('?edit'));
|
||||||
const redirect = res.redirects[0].split(' ')[1];
|
cy.window().then((win) => {
|
||||||
cy.visit(url);
|
|
||||||
cy.get('#view-mode-toggle-edit').click({force: true});
|
// Write Content
|
||||||
cy.get('.CodeMirror-scroll').type('{ctrl}a{backspace}');
|
cy.get('.CodeMirror-scroll').type('{ctrl}a{backspace}');
|
||||||
cy.get('.CodeMirror-scroll').type(`Moved to [${redirect}](${redirect})`);
|
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', () => {
|
describe('Migrate document', () => {
|
||||||
it('passes', async () => {
|
it('passes', async () => {
|
||||||
const baseUrl = 'https://md.margau.net';
|
|
||||||
const url = 'https://md.margau.net/H0JO3L5DS-6Yhv4RrdS-tw';
|
// Read list of pads to migrate
|
||||||
migrateDocument(url, baseUrl);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
10
cypress/fixtures/pads.json
Normal file
10
cypress/fixtures/pads.json
Normal 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"
|
||||||
|
}
|
||||||
|
]
|
Loading…
Add table
Reference in a new issue