There are a number of posts on this forum on the topic. A search might be helpful for you. I’d like to post a lengthier description of the issue soon, as it is of particular interest to me for several reasons, including that it is a struggle for me as well.
A general principle though, from the scholastics, is that the rise of a passion against one’s active will is never sinful (provided it doesn’t incite to some external act that is sinful or cause the will to succumb to it). Skipping way ahead in the argument (past the description of the principles in the soul and body that are involved here, which are deeply complex), using that same principle more or less applies in this case. You do not have these thoughts because you want to have them, you have them because you don’t want to have them, particularly the blasphemous ones. Impure thoughts can involve venial sin, once the passions begin to rise… There is no mortal sin here without the express desire to commit some external act, (which would include the deliberate use of the rising passion to influence the feelings of one’s own body in certain ways,) even if you are unable to at the time, i.e. “I would actually really truly do x, but I’m in public, and that’s the only reason I would not.” (This is “non-morose delectation” vs. “morose delectation.” Try dropping that one on your next confessor, see if he reads his manuals!

) The best tactic is to move your mind to something else, in both cases.
Another consideration is that blasphemy, in its strict sense, is an outward act. Inward acts of blasphemy would take their measure from their equality with an outward act, viz. actually saying these words occurring to the imagination vs. actually meaningfully saying them in my mind in an unqualified way, just as they would be if I actually spoke them (a little like the morose/non-morose distinction, but not entirely the same). We are here speaking of “imprecatory blasphemy,” which is language expressly used to spite God or injure His name or glory, etc. We can take a simple litmus test here: “Why do I not say out loud what occurs to my mind, even if it occurs forcefully? Is it because others will look at me funny, because I just had my tonsils taken out, or because I intuit by foreknowledge of what is about to come to my mind THAT I WILL NOT TO SAY THESE THINGS AND THEREFORE CAN’T POSSIBLY MEAN THEM, so it makes no sense to voice them since the outward voice is a sign of what is truly held within?”
One last brief consideration. Perhaps our will sometimes really does cave a little to these blasphemous thoughts, because “oh my good golly gosh they are sooooo annoying and maybe if I cave they’ll go away a little? Idk…” Supposing that the language used actually is attached to its object in a meaningful way, (which, even in these cases, it usually would not be, as these sorts of thoughts are typically devoid of any true communicative value and are “for their own sake,” as it were,) it would still not necessarily be mortal sin, though it would be at least venial. It takes true advertence to the meaning and moral import of what one is saying to make the act perfect. In other words, you have to realize how serious what you are saying is. This usually means deliberation, although it is possible that it doesn’t. That would normally require the formation of a “blasphemous habit,” like one might find on an atheist forum of some kind (your typical habit of dropping “JC” would fail to meet the mark here, in my opinion).
The sin of blasphemy is worse than murder. To put it in perspective. Take a deep, diaphragmatic breath or 40, and calm down.
More to come.