Is any function taking compact sets to compact sets, and connected sets to connected sets, necessarily continuous? It is well-known that continuous image of any compact set is compact, and that continuous image of any connected set is connected.
How far is the converse of the above statements true?
More precisely: Let $X$ be a topological space. Suppose there is a map $f$ from $X$ into itself which takes compact sets to compact sets and connected sets to connected sets. Do these conditions imply that $f$ is continuous?
If not in general, at least when $X$ is a metric space? or, when $f$ is a bijection? or both? or under any other additional conditions?
My apologies if my question in boringly elementary.
Thanks in advance!
 A: There are highly non-trivial positive results. 
Let us call a function $f$ from a space $X$ into a space $Y$ preserving if the image of every compact subspace of $X$ is compact in $Y$ and the image of every connected subspace of $X$ is connected in $Y$. 
McMillan  [On continuity conditions for functions, Pacific J. Math. 32 (1970) 479-494] proved the following result:
Theorem: If $X$ is Hausdorff, locally connected and Fréchet, and $Y$ is Hausdorff (e.g. if $X=Y=\mathbb R$), then any preserving function $f:X\to Y$ is continuous.
For further  results see Gerlits, Juhasz, Soukup, Szentmiklossy: Characterizing continuity by preserving compactness and connectedness, Top. Appl, 138 (2004), 21-44
Our main result is the following:
Theorem:  If $X$ is any product of connected linearly ordered spaces (e.g., if $X = \mathbb R^\kappa$ ) and $f:X \to Y$ is a preserving function into a regular space $Y$, then $f$ is continuous.
A: Replace [connected sets map to connected sets] with [point preimages are closed], and we have the following.
Theorem. Suppose $X$ and $Y$ are compactly generated weakly Hausdorff spaces and suppose $f:X\rightarrow Y$ is a function. 
Then $f$ is continuous if and only if conditions 1) and 2) hold:
1) $f(C)$ is compact for each compact Hausdorff $C \subset X$. 
2) $f^{-1}(y)$ is closed for each $y\in Y$.
Proof. The crucial case is when $X$ and $Y$ are compact Hausdorff. Brian Scott posted a nice proof.
https://math.stackexchange.com/questions/1527612/a-characterizion-of-continuity-for-functions-between-hausdorff-compacta
Recall the definition of a compactly generated weakly Hausdorff space (CGWH). The space $X$ is CGWH provided
A) $g(K) \subset X$ is closed in $X$ for each compact Hausdorff space $K$ and each map $g:K \rightarrow X$.
B) $A \subset X$ is closed in $X$ iff $A \cap C$ is closed in $X$ for all compact Hausdorff subspaces $C \subset X$. 
Consequently to test continuity of $f$ it suffices to test the restrictions $f \mid C$ to compact Hausdorff subspaces $C \subset X$. 
https://neil-strickland.staff.shef.ac.uk/courses/homotopy/cgwh.pdf.
