DEV Community

loading...
Cover image for I Created a CLI to Cross-Post Your Articles On Dev, Hashnode, and Medium

I Created a CLI to Cross-Post Your Articles On Dev, Hashnode, and Medium

shahednasser profile image Shahed Nasser Originally published at blog.shahednasser.com Updated on ・4 min read

This article was originally published on my personal blog

If you own a blog like me, but also cross-post your articles on platforms like Dev, Hashnode, and Medium, it gets tedious to post your articles in different places.

So, I created a simple CLI to cross-post my articles on these platforms easily. You can find it onNPMand check the code onGitHub.


Installation

In your terminal:

npm i -g cross-post-blog
Enter fullscreen mode Exit fullscreen mode

Usage

Set Configuration

For the simplicity of the CLI, and considering most of the APIs of each of the platforms do not allow or provide endpoints for user authentication, you will need to get your access tokens, API keys, or integration tokens from your own profile before using cross-post. This will just need to be done the first time or if you want to change the tokens.

The tokens are all stored on your local machine.

Here's a guide on how to do this for each of the platforms:

dev.to

After logging into your account on dev.to, click on your profile image and then click on Settings

Settings

Then, click on the Accounts tab in the sidebar

Accounts

Scroll down to the "DEV Community API Keys" section. You need to generate a new key. Enter "Cross Post" in the description text box or any name you want then click "Generate API key"

Generate API Key

Copy the generated API key, then in your terminal:

cross-post config dev
Enter fullscreen mode Exit fullscreen mode

You'll be prompted to enter the API key. Paste the API key you copied earlier and hit enter. The API key will be saved.

Hashnode

After logging into your account on Hashnode, click on your profile image and then click on "Account Settings"

Settings

In the sidebar click on "Developer"

Developer

Click the "Generate" button and then copy the generated access token.

Generate

Run the following in your terminal:

cross-post config hashnode
Enter fullscreen mode Exit fullscreen mode

First, you'll be prompted to enter your access token. Then, you need to enter your Hashnode username. The reason behind that is that when later posting on hashnode your publication id is required, so your username will be used here to retrieve the publication id. Once you do and everything goes well, the configuration will be saved successfully.

Medium

After logging into Medium, click on your profile image and then click on "Settings"

Settings

Then click on "Integration Tokens" in the sidebar

Integration Tokens

You have to enter the description of the token then click "Get integration token" and copy the generated token.

Generate token

In your terminal run:

cross-post config medium
Enter fullscreen mode Exit fullscreen mode

Then enter the integration token you copied. A request will also be sent to Medium to get your authorId as it will be used later to post your article on Medium. Once that is done successfully, your configuration will be saved.


Cross-Posting Your Articles

To cross-post your articles, you will use the following command:

cross-post run <url> [options]
Enter fullscreen mode Exit fullscreen mode

Whereurl is the URL of your article that you want to cross-post.options can be:

  1. -p, --platforms [platforms...] The platform(s) you want to post the article on. By default, if this option is not included, it will be posted on all the platforms. An example of its usage:
cross-post run <url> -p dev hashnode
Enter fullscreen mode Exit fullscreen mode
  1. -t, --title [title] The title by default will be taken from the URL you supplied, however, if you want to use a different title you can supply it in this option.
  2. -s, --selector [selector] by default, the article selector will be used to find your article in the URL you pass as an argument. However, if you need a different selector to be used to find the article, you can pass it here.

This command will find the HTML element in the URL page you pass as an argument and if found, it will extract the title (if no title is passed in the arguments) and cover the image.

It should be noted that on all platforms the article will be posted as a draft, however, due to the limitations of the Hashnode API, it will be posted as "hidden from Hashnode" but it will be public in your publication.

UPDATE: In the latest version of this library, you can now pass the option -pu, --public to publish the article publicly.


Conclusion

If you find any bugs or have any ideas you would like to contribute, please do so on the issues tab in theGitHub Repository!

Discussion (17)

Collapse
unfor19 profile image
Meir Gabay

I'm curious regarding the Medium integration. Posting to dev.to is easy since they fully support MarkDown, while Medium is .. Medium. Have you managed to overcome the "cross platform text editor compatibility" challenge?

Collapse
shahednasser profile image
Shahed Nasser Author

Medium allows you to choose the type of content through a parameter “contentFormat” and you can pass it markdown or html. I’m passing it markdown and it works pretty well

Collapse
unfor19 profile image
Meir Gabay

Does it also work for code blocks? Because if it does then wow man

Thread Thread
shahednasser profile image
Shahed Nasser Author

Yea it does. To be honest this part of work isn’t done by me as I’m using a library to transform the html to markdown, so it’s the one that’s doing this part correctly!

Thread Thread
unfor19 profile image
Meir Gabay

Super cool, then I gotta check it out :)

Collapse
inhuofficial profile image
InHuOfficial

great project, thanks for sharing! ❤ 🦄 🔖!

One super tiny thing, your first image for dev.to points the red arrow at "sign out" not at "settings".

I hope I find some time this week to give it a go!

Collapse
shahednasser profile image
Shahed Nasser Author

Oh thanks for pointing that out I thought it was obvious that it was pointing at the settings. I’ll make sureto fix that

Collapse
inhuofficial profile image
InHuOfficial

I mean this image for clarity:

image pointing to sign out instead of settings

It is obviously pointing at sign out (did you mean it was obvious you meant settings, as I completely agree then and I did say a super minor point!). The problem with the written word!

Anyway once again thanks for sharing a great CLI!

Thread Thread
shahednasser profile image
Shahed Nasser Author

Yes that’s what I meant. I’ll make sure to fix that. If you use it I’d also appreciate the feedback!

Thread Thread
inhuofficial profile image
InHuOfficial

Will do!

Collapse
ben profile image
Ben Halpern

Tools like this are great, nice stuff. Definitely why we enable APIs at DEV.

Collapse
msamgan profile image
Mohammed Samgan Khan

nice one, but can you please give me more detail as which is the base article.
like for the canonical URL.
it has to be one of the above three ?
is there any way to ingratiate it with custom blogs.

Collapse
shahednasser profile image
Shahed Nasser Author

I’m not sure if I correctly understand you, but the base article is your own blog. You pass it in the command cross-post run and then it is posted to these 3 platforms or whichever you choose from them.

Collapse
msamgan profile image
Mohammed Samgan Khan

thats nice.
really cool. i can initgrate it in my personal blog and everytime i publish to my blog it will automate the publishing on these three...
thats super cool
you are genius...

Thread Thread
shahednasser profile image
Shahed Nasser Author

Thank you! Currently it will publish them as a draft but I will be adding an option to publish them publicly soon.

Thread Thread
msamgan profile image
Mohammed Samgan Khan

may i have a one on one conversation on you regarding a project of mine if you don't mind..

Collapse
dangoslen profile image
Dan Goslen

Very cool! I'll give this a try today!

Forem Open with the Forem app