The advice about taking medication during pregnancy that everybody hears repeatedly (don't take anything, ever, basically) is erring on the side of caution, but there are circumstances which override this, and one of those circumstances is the safety and health (including mental health) of the mother.
If you were in a car crash (hypothetically) whilst you were pregnant then the doctors who were treating you in A&E for your hypothetical broken arm would use medication that wouldn't otherwise be advised in pregnant women. The same applies if you were to get a serious infection which needed something stronger than penicillin, the same applies to women who have epilepsy or diabetes and the same applies if your mental health is sufficiently serious that the medical professionals think that you need medication to bring you back to health.
I know at least 2 women who have had antidepressants during pregnancy, one of whom also had valium for panic attacks that she had during the later stages of pregnancy. Both got through their pregnancies fine and their babies were fine too. In neither case did they actually WANT to go on the pills, but they understood that allowing their mental health issues to spiral out of control was not in the interests of either themselves or their babies.
There is at least one thread on here about prenatal depression and taking antidepressants in that context, if you want to search for it.
I hope you find the right balance for you