Skip to content

How to mass edit post content?

Uncategorized
  • Heya everyone. Not long ago our forum migrated to NodeBB using the old migration tool. As part of that we changed all the old bb style [code] blocks, but I made a mistake when doing it and there are a bunch of blocks that use [c] as an alias for [code].

    How do I mass edit all posts, replacing "[c]" with "\n```\n"? I am more than happy to use the mongo shell if that's an option.

    ~ Laz

  • Heya everyone. Not long ago our forum migrated to NodeBB using the old migration tool. As part of that we changed all the old bb style [code] blocks, but I made a mistake when doing it and there are a bunch of blocks that use [c] as an alias for [code].

    How do I mass edit all posts, replacing "[c]" with "\n```\n"? I am more than happy to use the mongo shell if that's an option.

    ~ Laz

    The below custom script should do what you need. Make a backup of your database and place the script in your nodebb folder. Then you can run it with nodeb script_name.js

    /* eslint-disable no-await-in-loop */
    /* globals require, console, process */
    
    'use strict';
    
    const nconf = require('nconf');
    
    nconf.file({
    	file: 'config.json',
    });
    
    nconf.defaults({
    	base_dir: __dirname,
    	views_dir: './build/public/templates',
    	upload_path: 'public/uploads',
    });
    
    const db = require('./src/database');
    
    db.init(async (err) => {
    	if (err) {
    		console.log(`NodeBB could not connect to your database. Error: ${err.message}`);
    		process.exit();
    	}
    
    	await fixPosts();
    	console.log('done');
    	process.exit();
    });
    
    
    async function fixPosts() {
    	const batch = require('./src/batch');
    	await batch.processSortedSet('posts:pid', async (pids) => {
    		const postData = await db.getObjects(pids.map(pid => `post:${pid}`));
    		const bulkSet = [];
    		postData.forEach(p => {
    			if (p && p.content) {
    				bulkSet.push([
    					`post:${p.pid}`,
    					{
    						content: String(p.content).replace(/\[c\]/g, '\n```\n'),
    					},
    				])
    			}
    		});
    
    		await db.setObjectBulk(bulkSet);
    	}, {
    		batch: 500,
    	});
    }
    
  • Heya everyone. Not long ago our forum migrated to NodeBB using the old migration tool. As part of that we changed all the old bb style [code] blocks, but I made a mistake when doing it and there are a bunch of blocks that use [c] as an alias for [code].

    How do I mass edit all posts, replacing "[c]" with "\n```\n"? I am more than happy to use the mongo shell if that's an option.

    ~ Laz

    Much appreciated! I'll give that a run later tonight.

    I am a little confused about how to run the script though. Is nodeb a typo for nodebb? If it is, I don't think i can run commands via ./nodebb [script], I just get error: unknown command as a response. If it's not a typo, what is it?

    Many thanks.

  • Much appreciated! I'll give that a run later tonight.

    I am a little confused about how to run the script though. Is nodeb a typo for nodebb? If it is, I don't think i can run commands via ./nodebb [script], I just get error: unknown command as a response. If it's not a typo, what is it?

    Many thanks.

    @Laz it was a typo, you can run the script with node script_name.js, which invokes the node.js interpreter.

Diese Artikel könnten Dich auch interessieren.