add auto link

This commit is contained in:
xmcp
2018-08-21 13:03:58 +08:00
parent 35e6a08ec9
commit 3497c664d5
8 changed files with 78 additions and 14 deletions

View File

@@ -1,10 +1,21 @@
import React, {Component} from 'react';
import TimeAgo from 'react-timeago';
import Linkify, {linkify} from 'react-linkify';
import chineseStrings from 'react-timeago/lib/language-strings/zh-CN';
import buildFormatter from 'react-timeago/lib/formatters/buildFormatter';
import './Common.css';
const chinese_format=buildFormatter(chineseStrings);
const PID_RE_TEXT=/(?:^|[^\d])(\d{5,6})(?!\d)/g;
linkify.add('#', {
validate: /^(\d{5,6})/,
normalize: (match) => {
match.url='#'+match.url;
},
});
function pad2(x) {
return x<10 ? '0'+x : ''+x;
@@ -28,4 +39,12 @@ export function TitleLine(props) {
<span>{props.text}</span>
</p>
)
}
export function AutoLink(props) {
return (
<Linkify properties={{target: '_blank'}}>
<pre>{props.text.replace(new RegExp(PID_RE_TEXT,'g'),' #$1 ')}</pre>
</Linkify>
)
}